I have a C# application that contains multiple classes with properties that correspond to entries in a Json file.
I would like to validate the Json file against a Json schema which I will create using NewtonSoft's schema generator for types e.g.
JSchema schema = generator.Generate(typeof(ClassName)).
My problem is that this will generate one Json schema per class and I want to validate the Json file against all of them.
How can I combine the Json schemas into one big super schema?
After generation of schemas, If you want the schemas to make one then you can use this oneOf and $ref methods.
{
"oneOf": [
{"$ref": "your path here"}, // the root schema, defining the object
{
"type": "array", // the schema.
"items": {"$ref": "your path here"}
}
]
}
Related
I have some YAML files, and I want to define a schema and then validate the YAML against the schema before trying to work with it in C#.
From my research there doesn't appear to be a schema language specific for YAML, but people use JSON Schema to validate YAML files. So that's what I tried.
The Yaml library I'm using (YamlDotNet) doesn't appear to be able to do schema validation, so I tried to do what I saw in some other examples. Convert the YAML to JSON then use Newtonsoft.Json to validate against my schema.
var schemaJson = File.ReadAllText(schemaPath);
var schema = Newtonsoft.Json.Schema.JSchema.Load(new JsonTextReader(new StringReader(schemaJson)));
var deserializer = new YamlDotNet.Serialization.Deserializer();
var data = deserializer.Deserialize(new StringReader(yaml));
var sb = new StringBuilder();
var sw = new StringWriter(sb);
var serializer = new Newtonsoft.Json.JsonSerializer();
serializer.Serialize(sw, data);
Newtonsoft.Json.Linq.JObject.Parse(sb.ToString()).Validate(schema);
The problem with this is when I deserialize the YAML, it stuffs the data into a Dictionary, and ultimately every value is a string. So when I serialize back to JSON all the values get wrapped in single quotes, and when I try to validate against my schema, anything that the schema says is an integer/number will fail.
e.g. This YAML:
name: John Doe
age: 27
will get converted to JSON something like this:
{
name: 'John Doe'
age: '27'
}
And when I try to validate against my JSON Schema that says age should be an integer that fails.
Yes, you can use JSON Schemas to validate data from YAML files, as the data model that YAML is uses is very close (but not identical) to JSON. And most JSON Schema evaluators tend to work with deserialized data anyway, after it has been decoded from JSON or any other data format that uses a similar data model.
Your problem sounds like a misconfiguration in your YAML deserializer. The YAML format is capable of differentiating strings from numbers, so the serializer should do so as well, or at least offer different options for handling strings that look like numbers.
I have the next issue: I receive a lot of JSONs in different formats. I need to process only part of these documents. I tried to use Newtonsoft.Json.Schema nuget, but got the next problem:
JSON how to don't parse additional properties
Can you suggest me some ways to parse only part of the JSON document, when we don't know structure of this json? We can store some schema of the document.
Example:
We have the next JSON document.
And here we need to process, for example, only name and age properties. And I will know these properties only in runtime.
{
'name': 'James',
'age': 29,
'salary': 9000.01,
'jobTitle': 'Junior Vice President'
}
If your json does not contain nested fields, so each of the top level fields are primitive types (not objects) then you can deserialize it as Dictionary<string, object> like this:
var fieldValueMap = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
string fieldName = "name";
Console.WriteLine(fieldValueMap.ContainsKey(fieldName)
? fieldValueMap[fieldName]
: "There is no such field");
I generate object with EF and SQL to LINQ, and have complexs types which is returned from stored procedure. Now want to serialize those objects to nested JSON file, by using Newtonsoft json librarys.
The problem which have is that all serialized JSON files are flatten (cause returned result from procedures are all rows), now what want to ask is -
What tehnics to use to get nice structured (nested) JSON automatically (I need to serialize lot of procedure)?
Is there way to configure EF or SQL to LINQ to have functionality like polymorphic associations (like this, but that's old)
Example:
[{"key":value,"key":value,"key":value...}] --> generated JSON
Want get to look like:
{
"key": value,
"key": value,
.
.
.
},
"table1": <------ structured like this
{
"key": value
"key": value
},
"table2":
{
"key": value
"key": value
}
}
var details= _clientService.GetAsync<DoctorDetails>(getDetails).Result;
I get the Result from the service which is JSON when I use "object" in the GetAsync instead of DoctorDetails. However, I don't see any property values being filled in details (All are null in DoctorDetails). DoctorDetails is the cs file of the schema I generated through xsd.
DoctorDetails is an auto generated file that contains properties like
Name
ID etc
How to deserialize this and get values in those properties (in the details variable above)
Edit
It is only returning values if I make the syntax like this
var details= _clientService.GetAsync<object>(getDetails).Result;
If you haven't already tried this option, use a library called Json.Net from Newtonsoft for json stuff. Newtonsoft json.
Provided you have the schema details and the property names match you may try the following..
var details= _clientService.GetAsync<object>(getDetails).Result;//Please check if this is a string else use .ToString()
/*
"{
'Name': 'Doctor Who',
'ID' : '1001'
}";
*/
DoctorDetails m = JsonConvert.DeserializeObject<DoctorDetails>(details);
Documentation Deserialize an Object.
I'm not promoting this library, it's just a suggestion. :)
Hope it helps.
I have to create an XML document that would be based on an certain XML Schema Document. Since my data is a DataSet, I need to find the best way to start off.
I have couple of different ideas how to start:
manually create nodes, elements, attributes that would match XSD
transform DataSet into a class that would match the schema document and serialize it
something else?
Is this a right way to get a XML output from DataSet to match XSD schema?
May be you should give XMLBeans a try... It's a diverse framework for playing around with compiled XSD schemas. Compiled in this context means, you create JAVA classes from your XSD-files.
Compilation example (as can be seen here) scomp -out purchaseorder.jar purchaseorder.xsd
With this jar in your classpath you could create new a priori valid instances of your schema with something like:
public PurchaseOrderDocument createPO() {
PurchaseOrderDocument newPODoc = PurchaseOrderDocument.Factory.newInstance();
PurchaseOrder newPO = newPODoc.addNewPurchaseOrder();
Customer newCustomer = newPO.addNewCustomer();
newCustomer.setName("Doris Kravitz");
newCustomer.setAddress("Bellflower, CA");
return newPODoc;
}
You can find the whole example at: XMLBeans Tutorial under the heading "Creating New XML Instances from Schema".