using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
public static class JsonExtensions
public static T DeserializeEmbeddedJsonP<T>(Stream stream)
using (var textReader = new StreamReader(stream))
return DeserializeEmbeddedJsonP<T>(textReader);
public static T DeserializeEmbeddedJsonP<T>(TextReader textReader)
using (var jsonReader = new JsonTextReader(textReader.SkipPast('(')))
var settings = new JsonSerializerSettings
CheckAdditionalContent = false,
return JsonSerializer.CreateDefault(settings).Deserialize<T>(jsonReader);
public static class TextReaderExtensions
public static TTextReader SkipPast<TTextReader>(this TTextReader reader, char ch) where TTextReader : TextReader
public static void Test()
Console.WriteLine("Input JSON: ");
Console.WriteLine(Encoding.UTF8.GetString(bytes));
using (var stream = new MemoryStream(bytes, false))
var myType = JsonExtensions.DeserializeEmbeddedJsonP<MyType>(stream);
Console.WriteLine("\nDeserialized and re-serialized {0}:", myType);
Console.WriteLine(JsonConvert.SerializeObject(myType, Formatting.Indented));
using (var textReader = new StringReader("").SkipPast('('))
var s = textReader.ReadToEnd();
using (var textReader = new StringReader("hello").SkipPast('('))
var s = textReader.ReadToEnd();
using (var textReader = new StringReader("(((").SkipPast('('))
var s = textReader.ReadToEnd();
Assert.IsTrue(s == "((");
var json = @"parseResponse({
return Encoding.UTF8.GetBytes(json);
public static void Main()
Console.WriteLine("Environment version: " + Environment.Version);
Console.WriteLine("Json.NET version: " + typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine("Failed with unhandled exception: ");
public class AssertionFailedException : System.Exception
public AssertionFailedException() : base() { }
public AssertionFailedException(string s) : base(s) { }
public static class Assert
public static void IsTrue(bool value)
public static void IsTrue(bool value, string message)
throw new AssertionFailedException(message ?? "failed");