Can't import JSON file when referencing to another file - c#

I'm using NSwag.OpenaPiDocument.FromJsonAsync() to load a JSON file.
The JSON file is seperated in several files. Unfortunately, I'm getting this error:
Could not resolve the JSON path 'parameters.json#/tagsParam' with the full JSON path 'C:\Users\ribuss\Desktop\Swagger\json\petstore-separate\spec\parameters.json#/tagsParam'
I'm trying to import the following files (by calling the method for swagger.json)
In my application I use the following command:
NSwag.OpenApiDocument.FromJsonAsync(content, "C:/Users/ribuss/Desktop/Swagger/json/petstore-separate/spec/swagger.json").Result; // content is the full json text of swagger.json
Edit:
This is the schema which doesn't work:
{
"tagsParam": {
"name": "tags",
"in": "query",
"description": "tags to filter by",
"required": false,
"type": "array",
"collectionFormat": "csv",
"items": {
"type": "string"
}
},
"limitsParam": {
"name": "limit",
"in": "query",
"description": "maximum number of results to return",
"required": false,
"type": "integer",
"format": "int32"
}
}
I've changed the schema by using definitions and it's working now.

Related

How to compile avro schema from an avsc that contains references using avrogen (C#)?

I have these two avsc files, the first makes a reference to the second but when trying to compile using avrogen an error is returned and the schema is not generated.
Error:
Exception occurred. Undefined name: AnalogProperty at 'fields[0].type.items.fields[2].type.fields[0].type'
avsc 1
"doc": "Measurement Type, Analog or Discrete",
"name": "AnalogProperties",
"type": {
"fields": [
{
"doc": "",
"name": "RealPower",
"type": "AnalogProperty"
}
}
avsc 2
{
"name": "AnalogProperty",
"namespace": "Kafka.AvroSchemas",
"doc": "AnalogProperty object",
"type": "record",
"fields": [{
"doc": "Circuit from requested topology.",
"name": "cValue",
"type": "double"
},
{
"doc": "Circuit from requested topology.",
"name": "cTimestamp",
"type": "string"
},
]
}

How to validate json with json schema in NJsonSchema c#

As part of contract tests I have to validate json response I got from rest-endpoint against json-schema present in a file. I'm using NJsonSchema and was not able to perform this.
Json-schema in file is as something below
{
'type': 'object',
'properties': {
'remaining': {
'type': 'integer',
'required': true
},
'shuffled': {
'type': 'boolean',
'required': true
}
'success': {
'type': 'boolean',
'required': true
},
'deck_id': {
'type': 'string',
'required': true
}
}
}
Json I have to validate is something like below
{ 'remaining': 52, 'shuffled': true, 'success': true, 'deck_id': 'b5wr0nr5rvk4'}
Can anyone please throw some light (with examples) on how to validate json with jsonschema using NJsonSchema or Manatee.Json.
Disclaimer: I'm the author of Manatee.Json.
That looks like a draft-03 schema (the required keyword was moved out of the property declaration in draft-04). I'm not sure if NJsonSchema supports schemas that old; Manatee.Json doesn't.
JSON Schema is currently at draft-07, and draft-08 is due out soon.
My suggestion is to rewrite the schema as a later draft by moving the required keyword into the root as a sibling of properties. The value of required becomes an array of strings containing the list of properties that are required.
{
"type": "object",
"properties": {
"remaining": { "type": "integer" },
"shuffled": { "type": "boolean" },
"success": { "type": "boolean" },
"deck_id": { "type": "string" }
},
"required": [ "remaining", "shuffled", "success", "deck_id" ]
}
By doing this, it'll definitely work with Manatee.Json, and I expect it'll work with NJsonSchema as well.
If you have specific questions about using Manatee.Json, hit me up on my Slack workspace. There's a link on the GH readme.

How to dynamically Generate a class in C# [duplicate]

This question already has answers here:
Generating DLL assembly dynamically at run time
(4 answers)
Closed 4 years ago.
I have json data whose name is Elements and it has one field Property.Now I want to add those Property inside a class and that class needed to be generated dynamically.
Here is the structure of my json data.
"Elements": [
{
"$type": "PCL.DatePicker, PCL",
"Type": "DatePicker",
"SelectedDate": "2017-02-23T00:00:00",
"LabelText": "Date:",
"Property": "Date1",
"Visibile": true
},
{
"$type": "PCL.FormEntryField, PCL",
"Type": "Entry",
"Text": "",
"LabelText": "Full Name:",
"Property": "FullName",
"Visibile": true
},
{
"$type": "PCL.Picker, PCL",
"Type": "Picker",
"DefaultIndex": 0,
"Values": [ "English", "Hindi", "French", "Chinese", "Arabi" ],
"LabelText": "Language",
"Property": "Language",
"Visibile": true
},
{
"$type": "PCL.FormSwitch, PCL",
"Type": "Switch",
"DefaultValue": true,
"Value": false,
"Text": null,
"Property": "Happy",
"LabelText": "Are You Happy?",
"Visibile": true
},
{
"$type": "PCL.TimePicker, PCL",
"Type": "TimePicker",
"SelectedTime": "12:30:00",
"LabelText": "Time:",
"Property": "TIme",
"Visibile": true
}
]
Is it possible to do this?
You could certainly use a T4 template to read through your JSON file and write out the c# class structure and properties.
That is assuming you will be generating at design time, similar to how entities are generated using edmx files.
Please see the Microsoft resource below for the basics of T4 templates.
msdn.microsoft.com/en-us/library/bb126445.aspx

NJsonSchema: JSON schemas with nested arrays generate incorrect C# types

I would like to use NJsonSchema to generate C# classes. The problem is that for nested arrays, the type ends up looking like this:
System.Collections.ObjectModel.ObservableCollection<System.Tuple<System.Linq.Enumerable+WhereSelectEnumerableIterator2[NJsonSchema.JsonSchema4,System.String]>>
My test code for generating the classes looks like this:
var schemaAsync = NJsonSchema.JsonSchema4.FromFileAsync(
#"<myPath>/MyFile.json");
var schema = schemaAsync.Result;
var generator = new NJsonSchema.CodeGeneration.CSharp.CSharpGenerator(schema);
var file = generator.GenerateFile();
System.IO.File.WriteAllText(
#"<myPath>/SomeClass.cs",
file);
I have two JSON schema files, there's the definition file (Def1.json):
{
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"InnerList": {
"description": "Only ever has 2 items",
"type": "array",
"items": [
{
"type": "integer",
"minimum": 0,
"maximum": 100,
"default": 0
},
{
"type": "integer",
"minimum": 0,
"maximum": 100,
"default": 0
}
],
"additionalItems": false
},
"OuterList": {
"type": "array",
"items": {
"$ref": "#/definitions/InnerList"
}
},
}
}
And a separate schema that makes use of the definition file:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "MySchema",
"type": "object",
"required": [ "OuterList" ],
"properties": {
"OuterList": { "$ref": "Def1.json#/definitions/OuterList" }
},
"additionalProperties": false
}
Am I using the library incorrectly?
This problem has been fixed in the latest version.

Deserializing Chrome Bookmark JSON Data in C#

In response to a question I asked a few days ago, I'm attempting to stretch myself a little, and do something that I've not really focussed on much before. I've done some searching (both here, and in general), but can't find the answers (or even reasonable hints) to what I want to achieve (though, a few things come close-ish).
Basically, I'm trying to deserialize the data for the Google Chrome bookmarks file using the Json.NET library (though, if there's a better alternative, I'm all for that - the documentation for this library is a little confusing in places). I'm a little confused as to the next step to take, due primarily to being used to PHP's fantastic handling of JSON data (using json_decode()), allowing for a single function call, and then simple associative-array access.
The library (Json.NET) wants me to specify an Object type that it can deserialize the JSON data into, but I'm not really sure how to go about structuring such an Object, given the format of the Bookmarks file itself. The format is something along the lines of:
{
"roots": {
"bookmark_bar": {
"children": [ {
"children": [ {
"date_added": "12880758517186875",
"name": "Example URL",
"type": "url",
"url": "http://example.com"
}, {
"date_added": "12880290253039500",
"name": "Another URL",
"type": "url",
"url": "http://example.org"
} ],
"date_added": "12880772259603750",
"date_modified": "12880772452901500",
"name": "Sample Folder",
"type": "folder"
}, {
"date_added": "12880823826333250",
"name": "Json.NET",
"type": "url",
"url": "http://james.newtonking.com/pages/json-net.aspx";
} ],
"date_added": "0",
"date_modified": "12880823831234250",
"name": "Bookmarks bar",
"type": "folder"
},
"other": {
"children": [ ],
"date_added": "0",
"date_modified": "0",
"name": "Other bookmarks",
"type": "folder"
}
},
"version": 1
}
Now, in PHP, I'd be far more used to doing something along the lines of the following, to get the data I wanted, and ending up with 'Json.NET':
$data['roots']['bookmark_bar']['children'][0]['name'];
I can work out, simply enough, what objects to create to represent the data (something like a root object, then a bookmark list object, and finally an individual bookmark object) - but I'm really not sure as to how to implement them, and then get the library to deserialize into the relevant objects correctly.
Any advice that can be offered would be greatly appreciated.
It is not necessary to declare a type that reflects the json structure:
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.IO;
using System;
class Program
{
static void Main(string[] args)
{
string json =
#"
{
""roots"": {
""bookmark_bar"": {
""children"": [ {
""children"": [ {
""date_added"": ""12880758517186875"",
""name"": ""Example URL"",
""type"": ""url"",
""url"": ""http://example.com""
}, {
""date_added"": ""12880290253039500"",
""name"": ""Another URL"",
""type"": ""url"",
""url"": ""http://example.org""
} ],
""date_added"": ""12880772259603750"",
""date_modified"": ""12880772452901500"",
""name"": ""Sample Folder"",
""type"": ""folder""
}, {
""date_added"": ""12880823826333250"",
""name"": ""Json.NET"",
""type"": ""url"",
""url"": ""http://james.newtonking.com/pages/json-net.aspx""
} ],
""date_added"": ""0"",
""date_modified"": ""12880823831234250"",
""name"": ""Bookmarks bar"",
""type"": ""folder""
},
""other"": {
""children"": [ ],
""date_added"": ""0"",
""date_modified"": ""0"",
""name"": ""Other bookmarks"",
""type"": ""folder""
}
},
""version"": 1
}
";
using (StringReader reader = new StringReader(json))
using (JsonReader jsonReader = new JsonTextReader(reader))
{
JsonSerializer serializer = new JsonSerializer();
var o = (JToken)serializer.Deserialize(jsonReader);
var date_added = o["roots"]["bookmark_bar"]["children"][0]["date_added"];
Console.WriteLine(date_added);
}
}

Categories

Resources