Manatee.Json

Provides an intuitive approach to JSON, including its structure, serialization, JSON Schema, JSON Path, JSON Pointer, and JSON Patch.


Keywords
json, jsonpath, jsonpointer, jsonschema, linq-to-json, manatee, parser, path, pointer, schema, serializer, transform
License
MIT
Install
Install-Package Manatee.Json -Version 1.1.1.1

Documentation

After careful consideration, I may be halting development for this library in favor of a new suite of libraries that provide the same functionality using the System.Text.Json namespace.

Thank you to all those who helped make this library better through code contributions, bug reports, and feature suggestions.

If you are interested in taking over development of this library, please DM me on Slack (link below) to discuss options.

Manatee.Json

NuGet version Build status AppVeyor tests Percentage of issues still open Average time to resolve an issue License

Discuss on Slack Made with Jetbrains Resharper

Documentation for this library can be found here.

The primary goal of Manatee.Json is to make working with JSON simple and intuitive for the developer. This library recognizes that JSON is much more than just a mechanism for data transfer.

Secondarily, Manatee.Json is intended to be strictly compliant with RFC-8259, which means that it purposefully does not support JSON variants, like single-quoted strings or BSON.

Read from a file

var text = File.ReadAllText("content.json");
var json = JsonValue.Parse(text);

or

using var reader = File.Open("content.json");
var json = JsonValue.Parse(reader); // also available as async

The json field now contains the content of the .json file. The object structure is exactly what you'd expect by looking at the file.

Object model

These JSON types map to primitive .Net types:

  • true/false as Boolean,
  • numbers as Double,
  • strings as String

These JSON types map to types defined in Manatee.Json:

  • objects ({"key":"value", "otherKey":9}) as JsonObject which derives from Dictionary<string, JsonValue>
  • arrays (["value", 9] as JsonArray which derives from List<JsonValue>

All of these types are encapsulated in a container type, JsonValue. This type exposes a property for each value type, as well as a property for which type the value contains.

For the JSON null there is a readonly static JsonValue.Null field.

Building JSON manually

Manatee.Json defines implicit conversions to JsonValue from Boolean, Double, String, JsonObject, and JsonArray. This helps greatly in building complex objects manually.

JsonValue str = "value",
          num = 10,
          boolean = false;

Because the collection types are derived from core .Net types, you also get all of the initialization capabilities.

var obj = new JsonObject
    {
        ["key"] = "value",
        ["otherKey"] = 9
    }
var array = new JsonArray { "value", 9, obj };

Serialization

Converting .Net objects to and from JSON is also simple:

  1. Create a serializer

     var serializer = new JsonSerializer();
    
  2. De/Serialize

     var myObject = serializer.Deserialize<MyObject>(json);
     var backToJson = serializer.Serialize(myObject);
    

There are many ways to customize serialization. See the wiki page for more details!

But wait, there's more!

Manatee.Json also:

  • Is covered by over 4000 unit tests
  • Conforms to RFC-8259: The JSON specification
  • Support .Net Standard 2.0
  • Outputs compact and prettified JSON text
  • Supports JSON Schema INCLUDED AND FREE!
    • Draft 4
    • Draft 6
    • Draft 7
    • Draft 2019-09 (a.k.a. draft 8)
    • Native object model
    • Output contains all info required to craft custom error messages
    • User-defined keywords
  • Supports JSONPath
    • Native object model
    • Compile-time checking
  • Supports JsonPatch (with object model)
  • Supports JSON Pointer (with object model)
  • Is fully LINQ-compatible
  • Converts between JSON and XML
  • Reports parsing errors using JSON Pointer to identify location
  • Supports streamed parsing
  • Is fully open-source under the MIT license

Serialization features:

  • De/Serialize abstraction types (abstract classes and interfaces) by type registration
  • De/Serialize dynamic types
  • JIT type creation for unregistered abstraction types
  • De/Serialize anonymous types
  • De/Serialize immutable types
  • Fully customizable serialization of both 1st- and 3rd-party types
  • De/Serialize static types/properties
  • De/Serialize fields
  • De/Serialize enumerations by name or numeric value
  • Maintain object references/graphs
  • De/Serialize circular references
  • Optionally include type names
  • Each serializer instance can be independently configured
  • Supports multiple date/time formats (ISO 8601, JavaScript, custom)
  • Supports using DI containers for object creation
  • Supports non-default constructors
  • Property name customization via attribute
  • Global property name transformations
  • Opt-out property inclusion via attribute
  • Optionally serialize only properties for requested type or all properties defined by object

See the docs for more information on how to use this wonderful library!

Contributing

If you have questions, experience problems, or feature ideas, please create an issue.

If you'd like to help out with the code, please feel free to fork and create a pull request.

Special thanks

@sixlettervariables (Christopher Watford) for digging around the muck that was the serialization code and drastically improving performance.

@Kimtho for finding and fixing some backwards logic in the JSON Patch replace verb.

@desmondgc (Desmond Cox) for improving the validation within schemas for date-time formatted strings.

The Project

This code uses C# 7 features, so a compiler/IDE that supports these features is required.

The library consists of a single project that target .Net Standard 1.3. The test project targets .Net Framework 4.6.

Building

During development, building within Visual Studio should be fine.

Code style and maintenance

I use Jetbrains Resharper in Visual Studio to maintain the code style (and for many of the other things that it does). The solution is set up with team style settings, so if you're using Resharper the settings should automatically load. Please follow the suggestions.

Appreciation

If you've enjoyed using this library and you'd like to contribute financially, please use the button below.

Donate