using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Collections.ObjectModel;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
public static partial class JsonExtensions
public static JToken LoadFromStream(Stream s, JsonLoadSettings settings = default, bool closeInput = true, FloatParseHandling? floatParseHandling = default, DateParseHandling? dateParseHandling = default)
using (var reader = new StreamReader(s))
using (var jsonReader = new JsonTextReader(reader) { CloseInput = closeInput })
if (floatParseHandling != null)
jsonReader.FloatParseHandling = floatParseHandling.Value;
if (dateParseHandling != null)
jsonReader.DateParseHandling = dateParseHandling.Value;
return JToken.Load(jsonReader, settings);
public static T Deserialize<T>(Stream s, JsonSerializerSettings settings = default, bool closeInput = true)
using (var reader = new StreamReader(s))
using (var jsonReader = new JsonTextReader(reader) { CloseInput = closeInput })
JsonSerializer ser = JsonSerializer.CreateDefault(settings);
return ser.Deserialize<T>(jsonReader);
public static void Test()
foreach (var json in GetJson())
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(json));
var token = JsonExtensions.LoadFromStream(stream, new JsonLoadSettings { });
Console.WriteLine(token);
IJsonLineInfo lineInfo = token;
Console.WriteLine("HasLineInfo: {0}", lineInfo != null && lineInfo.HasLineInfo());
foreach (var json in GetJson())
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(json));
var token = JsonExtensions.Deserialize<JToken>(stream, new JsonSerializerSettings { });
Console.WriteLine(token);
IJsonLineInfo lineInfo = token;
Console.WriteLine("HasLineInfo: {0}", lineInfo != null && lineInfo.HasLineInfo());
public static IEnumerable<string> GetJson()
@"""2009-02-15T00:00:00Z""",
@"""\uD867\uDE3D\""\\\/\b\f\n\r\t\u0121""",
""title"": ""example glossary"",
""GlossSeeAlso"": [""GML"", ""XML""]
{""value"": ""Close"", ""onclick"": ""CloseDoc()""}
return primitives.Concat(examples);
public static void Main()
Console.WriteLine("Environment version: {0} ({1})", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , GetNetCoreVersion());
Console.WriteLine("{0} version: {1}", typeof(JsonSerializer).Assembly.GetName().Name, typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine("Failed with unhandled exception: ");
public static string GetNetCoreVersion()
var assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly;
var assemblyPath = assembly.Location.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2)
return assemblyPath[netCoreAppIndex + 1];