3

I can not google that thing on json.net api reference or anywhere. I want to create object from json schema with default values filled in. Basically some thing like this:

var JsonSchema=JsonSchema.ReadSchemaFromSomeWhere();
dynamic DefaultObject= JsonSchema.GetDefaultObject();

Example you might see in json-schema-defaults package.

Example

var JsonSchema=JsonSchema.ReadSchemaFromString("
{
  "title": "Album Options",
  "type": "object",
  "properties": {
    "sort": {
      "type": "string",
      "default": "id"
    },
    "per_page": {
      "default": 30,
      "type": "integer"
    }
  }");

dynamic DefaultObject= JsonSchema.GetDefaultObject();

//DefaultObject dump is
{
  sort: 'id',
  per_page: 30
}

UPDATE

I want lib or api in json.net to create object with default values from any given valid json schema during runtime.

Kostia Mololkin
  • 868
  • 9
  • 25

1 Answers1

1

Well a simple case might be this

[Test]
public void Test()
{
   dynamic ob = new JsonObject();
   ob["test"] = 3;

   Assert.That(ob.test, Is.EqualTo(3));

}

I used the RestSharp library that provides a good dynamic implementation that allows indexing ["test"];

So then - what You're left to do is read the properties from the schema and assign values (of course this will work only for simple plain case`s, but might be a start

dynamic ob = new JsonObject();
foreach (var prop in JsonSchema.Properties)
{

   if (prop.Default != null)
      ob[prop.Name] = prop.Default
}
Marty
  • 3,485
  • 8
  • 38
  • 69
  • no , my intent is not get c# class code in order to compile, i want in runtime on any given vaiild json schema produce json object from it with default values, so im looking for lib or api in json.net, i will update my question to make it clear sorry – Kostia Mololkin Mar 09 '16 at 20:21
  • what would the defaults be ? a dynamic object, with props from schema set to NULL values ? – Marty Mar 09 '16 at 20:23
  • null in case schema not specified default value for property , if it specified default value then it should be there , check node package from reference in question ,it does what i want in javascript – Kostia Mololkin Mar 09 '16 at 20:25
  • yep, working on it. seems that there is no out of the box solution. at least from NewtonSoft – Marty Mar 09 '16 at 20:38
  • thanks for looking into it!, i checked source code of js lib - seems not too big, and might be rewritten in c# but still:) – Kostia Mololkin Mar 09 '16 at 20:40
  • can You post that js lib source URL ? – Marty Mar 09 '16 at 20:41
  • I added a simplified implementation, with a core solution being in RestSharp library, which allows indexing properties like ob["name"].. the rest I guess is in that js lib, or can be handled by making the function recursive (properties is a list of schema objects) – Marty Mar 09 '16 at 20:57
  • thanks for info, yes it seems good starting point ,as well as recursive it also need reference resolving – Kostia Mololkin Mar 09 '16 at 21:40
  • any progress with .net implementation? – Ivan Yuriev Jan 31 '19 at 10:59
  • @IvanYuriev we ended up implement simple recursive method on client to do this, given number of schema specification constructs we support method pretty simple and its easy to extend if you you want to support whole schema specification – Kostia Mololkin Jan 31 '19 at 19:15