Code Monkey home page Code Monkey logo

jsonnetmigrate's Introduction

A .NET library that assists with migrating from Json.NET to System.Text.Json.

Build

Overview

This library provides a class that implements a subset of the Json.NET JsonConvert class's API. It also provides some JsonConverter classes to use with the System.Text.Json library to make it act more like Json.NET.

The goal of this library is just to make it easier to migrate to System.Text.Json. What's available should mostly be compatible with Json.NET, but it is missing a lot of features that Json.NET provides.

Installation

You have a couple options for installing this library:

  • Install the NuGet package
  • Copy the source code directly into your project

This project is available under either of two licenses: the Json.NET MIT license or the Unlicense. The goal is to allow you to copy any of the source code from this library into your own project without having to worry about attribution or any other licensing complexity.

Note that the API and some comments are based on the Json.NET library, and some test files that are based on Json.NET code have their own License comment header.

Getting Started

Serializing an object works just like Json.NET with the JsonConvert.SerializeObject method:

using JsonNetMigrate.Json;

var value = new { a = 1, b = "two" };
var json = JsonConvert.SerializeObject(value);

Console.WriteLine(json);
{"a":1,"b":"two"}

This library also provides a subset of the JsonSerializerSettings class for customizing the Json output:

using JsonNetMigrate.Json;

var value = new Dictionary<string, object?> { ["a"] = 1.0, ["b"] = StringComparison.Ordinal, ["c"] = new DateTime(2002, 2, 2) };
var json = JsonConvert.SerializeObject(value, new JsonSerializerSettings
{
    Formatting = Formatting.Indented,
    NullValueHandling = NullValueHandling.Ignore,
    ContractResolver = new CamelCasePropertyNamesContractResolver(),
    Converters = new[] { new StringEnumConverter() },
});

Console.WriteLine(json);
{
  "a": 1,
  "b": "Ordinal",
  "c": "2002-02-02T00:00:00"
}

You can also parse a Json string back into objects with the JsonConvert.DeserializeObject method:

using JsonNetMigrate.Json;

var json = @"{""Formatting"":1,""NullValueHandling"":1}";
var value = JsonConvert.DeserializeObject<JsonSerializerSettings>(json);

Console.WriteLine($"Formatting = {value!.Formatting}");
Console.WriteLine($"NullValueHandling = {value!.NullValueHandling}");
Formatting = Indented
NullValueHandling = Ignore

The same JsonSerializerSettings class can be used for deserialization:

using JsonNetMigrate.Json;

var json = @"{""a"": 1.1,""b"": ""Ordinal"",""c"": null,""d"": ""2002-02-02T00:00:00""}";
var value = JsonConvert.DeserializeObject(json, new JsonSerializerSettings
{
    DateParseHandling = DateParseHandling.DateTimeOffset,
    FloatParseHandling = FloatParseHandling.Decimal,
});

var valueDictionary = (IDictionary<string, object>)value!;
foreach (var kv in valueDictionary)
{
    Console.WriteLine($"Key = {kv.Key}, Value = {kv.Value} (type {kv.Value?.GetType()?.FullName ?? "null"})");
}
Key = a, Value = 1 (type System.Int64)
Key = b, Value = Ordinal (type System.String)
Key = c, Value =  (type null)
Key = d, Value = 2/2/2002 12:00:00 AM (type System.DateTime)

Limitations

This library does not add support for the Json.NET serialization attributes (Newtonsoft.Json.JsonPropertyAttribute) or the DataMember attributes. That might implemented in the future after System.Text.Json version 5+ is released if it's made easier to implement object and collection converters.

This library does not include JObject, JArray, or JToken. This project uses IDictionary<string, object?> instead of JObject and IList<object?> instead of JArray.

Converters

The key to making this whole library work is the ObjectConverter. As of System.Text.Json version 4.7, parsing to object just gets you a raw JsonElement object (even for Json numbers, booleans, and strings). Using this ObjectConverter will get you much closer to the Json.NET behavior, with a couple differences:

  • Objects are parsed to Dictionary<string, object?> (as opposed to the Json.NET JObject, which implements IDictionary<string, JToken>)
  • Arrays are parsed to List<object?> (as opposed to the Json.NET JArray, which implements IList<JToken>)

jsonnetmigrate's People

Contributors

22222 avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.