Never having worked with Newtonsoft before, I read many threads here and found that producing the model object should be easy: use one of the many online tools to convert the schema to VB.net (or C# and convert). My attempts to do so have all failed, that also includes using a pure C# conversion. The issue has to do with collections (I think) or maybe just the schema file itself.
The original schema is 3500 lines, here’s a snippet (it’s NDA so I don’t know how much I can post). This is right near the top, it’s part of the definition of the root object:
"properties": {
"Properties": {
"type": "array",
"minItems": 1,
"uniqueItems": true,
"items": {
"$ref": "#/definitions/Property"
}
}
And then later in the code is a definition of the Property object, which is basically just a bunch of strings:
"Property": {
"type": "object",
"required": [
"Name",
"Location",
"ID",
"ObjectType"
],
"properties": {
"Messages": {
"type": "array",
"minItems": 1,
"uniqueItems": true,
"items": {
"$ref": "#/definitions/Message"
}
},
"Name": {
"$ref": "#/definitions/$basicType/definitions/requiredString"
},
"Location": {
"$ref": "#/definitions/$basicType/definitions/requiredString"
},
... more of same...
I read this as the root JSON has a structure called “Properties” that is an array of Property objects, which has a Messages collection (which are just strings) and then a bunch of fields. Looking at a sample file of the output JSON, I see a single object in the collection (as expected):
"Properties":[{"ID":"33...
First up, I tried jsonutils.com, as it has direct JSON->VB. That produced this:
Public Class Items
Public Property $ref As String
End Class
Public Class Properties
Public Property type As String
Public Property minItems As Integer
Public Property uniqueItems As Boolean
Public Property items As Items
End Class
Public Class Properties
Public Property Version As Version
Public Property Messages As Messages
Public Property Properties As Properties
End Class
....
Public Class Property
Public Property type As String
Public Property required As String()
Public Property properties As Properties
End Class
Among the many issues, it defined two classes called Properties. Moreover, the list, items
, is not a collection of Property objects but a reference to a new class called Items which is a ref to a Properties, which doesn’t refer to Property objects. This is not the only instance of an items
collection in the schema, and it converted them all to Items
despite them pointing to different underlying objects.
Next up, I tried the alternative suggestion of making a C# version using JSON2Csharp (now THAT’s funny, it autocorrected to “shart”!), and then converting that. Realizing the two-step process was unlikely to work out of the box, I instead made a new C# project and put the results in there. This too gave me many lines like…
public Items items { get; set; }
But this time it didn’t bother to make an Items class at all, so these are just errors.
OK, questions:
-
does anyone know of a tool that properly handles this sort of JSON schema? I assume it’s the $ or # that’s confusing it? I saw many references to various downloadable tools, but I can no longer find any of them online.
-
failing that, I suspect I simply have to convert the various items references to a collection type, but I’m not familiar with NS, so are these simply
List(Of Property)
or does it expect some other type during deserialization?
13
Google, having failed to turn up anything useful, did point me to the JSON Schema discord. A post there turns up the answer to this thread:
corvus-dotnet/Corvus.JsonSchema
Personally I would have preferred to use an online tool, but having installed this tool it immediately produced a working object model and I’ll definitely recommend it to everyone.
For those of you wondering, the advantage to using the schema file (aside from that being all I had) is that it contains much more metadata that is absolutely useful… things like which fields are required and which are optional, the types of the objects in an array, etc. These flesh out the object model much more than using an actual JSON file.