pekaaw
pekaaw

Reputation: 2607

Replacing System.Text.Json with Manatee.Json for serialization in .NET Core 3

Background
I want to provide some JsonSchema from my .NET Core 3 application, as well as other objects serialized into JSON. Since Manatee.Json is frequently updated and provides good support for JsonSchema, they are my preferred choice. At the same time, .NET Core 3 provides excellent support for returning objects with magic that transform them into JSON documents.

Example:

public ActionResult<MyFancyClass> MyFancyAction()
{
    return new MyFancyClass {
        Property1 = "property 1 content",
        Property2 = "property 2 content",
    };
}

Output:

{
    "Property1": "property 1 content",
    "Property2": "property 2 content"
}

Problem
.NET Core 3 has internal support for JSON with its System.Text.Json which is used in the previous example. If I try to serialize Manatee.Json.Schema.JsonSchema, its internal structure are serialized, not the json schema itself.

Example:

public ActionResult<MyFancyClass2> MyFancyAction2()
{
    return new MyFancyClass2 {
        Property1 = "property 1 content",
        Property1Schema = new JsonSchema()
            .Type(JsonSchemaType.String)
    };
}

Output:

{
  "Property1": "property 1 content",
  "Property1Schema": [{
    "name":"type",
    "supportedVersions":15,
    "validationSequence":1,
    "vocabulary": {
      "id":"https://json-schema.org/draft/2019-09/vocab/validation",
      "metaSchemaId":"https://json-schema.org/draft/2019-09/meta/validation"
    }
  }]
}

I expect this:

{
  "Property1": "property 1 content",
  "Property1Schema": {
    "type": "string",
  }
}

Manatee.Json.JsonValue also have a conflicting inner structure, where System.Text.Json.JsonSerializer fails to access internal get methods correctly and I get for instance this exception message:

Cannot access value of type Object as type Boolean.

Discovery
Manatee.Json.Schema.JsonSchema has a .ToJson() method that can be used to get the correct json schema as a JsonValue, but then I get the problem I just mentioned with serializing Manatee.Json.JsonValue.

Question
Does anyone know a way to change default serialization in .NET Core 3 from using System.Text.Json to using Manatee.Json?

Sidemark
Another way forward is to enable System.Text.Json to serialize Manatee.Json structures (take a look at this question).

Upvotes: 0

Views: 939

Answers (1)

gregsdennis
gregsdennis

Reputation: 8428

You're in luck! I just release the schema-successor to Manatee.Json: JsonSchema.Net! It has full support of drafts 6 through 2019-09, is based 100% in System.Text.Json, and it's ridiculously fast!

Hopefully this helps.

(Don't know why I didn't get emailed for the Manatee.Json tag. Sorry it took so long to find this.)

Upvotes: 1

Related Questions