0

I am new to JSON, but I understand C# a bit. I have a JSON schema and I ran across a part that I'm not sure how to put it in the objects class.

"def_subscore": {
    "type": "number",
    "minimum": 0,
    "maximum": 10
},
"def_impact": {
    "type": "object",
    "properties": {
        "baseMetricV3": {
            "type": "object",
            "properties": {
                "cvssV3": {"$ref": "cvss-v3.0.json"},
                "exploitabilityScore": {"$ref": "#/definitions/def_subscore"},
                "impactScore": {"$ref": "#/definitions/def_subscore"}
            }
        }
    }
}

As we can see baseMetricV3 is a class object and defined with it's type and properties. exploitabilityScore is supposed to be a "Subscore" which is defined as a number with min and max limitations.

Can I make a class that acts like a double/integer and also has to pass class-specific validations?

Is the schema just saying exploitabilityScore and impactScore are just double/integers that require validation in my code to make sure the value falls between 0 and 10?

Or is the schema just saying exploitabilityScore and impactScore are just double/integers, by the way the data should never come through with values outside of 0 and 10 (as in the validation was already done on their side and the def_subscore was more of an informative)?

Bagdan Gilevich
  • 1,231
  • 1
  • 11
  • 17
toadfromgrove
  • 143
  • 2
  • 14
  • 1
    json has no standard schema system. The example used is probably invented by a lib. So short answer is, no you can't do this automatically. Manual validation is required – Steve Aug 09 '18 at 16:14
  • @Steve Well it's not an official IEEE standard, but pretty sure, anyone talking about json schema is talking about this http://json-schema.org/ – derpirscher Aug 09 '18 at 16:16
  • So think of the JSON as just an export of structured data. It sounds like you expect JSON to actually serialize the class logic? Your not exporting the class logic, just the data properties. So there are no methods or constructors, and there is no if then else logic stored in the actual JSON. You cannot recreate a class on a different system from the exported JSON you can only load the class, but the other system needs to know what the class is or it will just load a simple POCO as a stand in. – Juls Aug 09 '18 at 16:37
  • @Juls you have to distinguish between the schema (as shown above) which describes the structure and constraints of an object and the data-object itself. It's like XML-Schema and XML. The schema just defines the properties and types (and possibly constraints on the types) but don't bear any data. A data object can then be validated if it complies to the schema (ie has all required properties with correct types, and the values follow the given constraints). Nobody is talking about methods, just properties, of course. – derpirscher Aug 09 '18 at 16:47
  • @derpirscher from my reading of the original question, it sounded like ~toadfromgrove, who says he is new to JSON, thought that JSON was going to serialize the actual class logic. I just wanted to make it clear that JSON isn't serializing the class, just the data properties. If you are new that's probably the first thing you need to know. – Juls Aug 09 '18 at 17:02

2 Answers2

0

If you schema says prop is of type number you have to define some double/int/float/... numerical type for that property and not a class.

If you only want to check, whether a JSON object complies to the given schema, you can use some json validator library to validate the object against the schema.

Additionally, (or if you for instance parse a JSON string to a self defined class) you can use the RangeAttribute for your class

class SomeClass {
    [Range(1, 10)]
    public int SomeValue {get;set;}
}

For other constraints there may be other Annotations. You can even define them yourself like shown here https://stackoverflow.com/a/7256854/3776927

derpirscher
  • 14,418
  • 3
  • 18
  • 35
  • so I chose to add the object class Subscore with the Range attribute and a public double value property. The only negative I can see with this is rather than updating the exploitabilityScore with "Impact.exploitabilityScore = ..." I just have to go one level deeper in the Impact object and set the Subscore.value = ... – toadfromgrove Aug 09 '18 at 18:47
0

I usually use VS Code with extension - Paste JSON as Code.

It provide flexibility to create class from json schema and json sample enter image description here

Nilesh Sawant
  • 1,406
  • 14
  • 8