I'm having an issue getting a json file I'm trying to create to use the json schema I've made so that I can make use of both intellisense and validation to ensure the json I have to hand-jam is good. I'm not sure if it's my schema that's wrong, or how I'm trying to get VS2015 to use it.
For now I've thrown all the schema stuff into one file (was having issues trying to split it up).
{
"$schema": "http://json-schema.org/schema#",
"id": "http://savatronix.com/jsonschemas/losthaven1/MainSchemaV1.json",
"title": "Lost Haven Common JSON",
"description": "A schema for json types that will be common across many different object types. V1",
"definitions": {
"gameDateTime": {
"type": "object",
"properties": {
"year": {
"type": "integer",
"minimum": 0
},
"month": {
"type": "string",
"enum": [ "Nil", "Spring", "Summer", "Autumn", "Winter" ]
},
"weekday": {
"type": "string",
"enum": [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ]
},
"day": {
"type": "integer",
"minimum": 0,
"maximum": 30
},
"hour": {
"type": "integer",
"minimum": 0,
"maximum": 23
},
"minute": {
"type": "integer",
"minimum": 0,
"maximum": 59
},
"second": {
"type": "integer",
"minimum": 0,
"maximum": 59
}
}
},
"characterAttributes": {
"type": "object",
"properties": {
"strength": {
"type": "integer",
"minimum": 0
},
"agility": {
"type": "integer",
"minimum": 0
},
"dexterity": {
"type": "integer",
"minimum": 0
},
"intelligence": {
"type": "integer",
"minimum": 0
},
"endurance": {
"type": "integer",
"minimum": 0
},
"charisma": {
"type": "integer",
"minimum": 0
},
"luck": {
"type": "integer",
"minimum": 0
}
}
},
"characterElements": {
"type": "object",
"properties": {
"earth": { "type": "integer" },
"wind": { "type": "integer" },
"fire": { "type": "integer" },
"water": { "type": "integer" },
"lightning": { "type": "integer" },
"light": { "type": "integer" },
"dark": { "type": "integer" }
}
},
"characterStatus": {
"type": "object",
"properties": {
"poison": { "type": "integer" },
"paralyze": { "type": "integer" },
"sleep": { "type": "integer" },
"fatigue": { "type": "integer" },
"charm": { "type": "integer" },
"confusion": { "type": "integer" }
}
},
"characterStats": {
"type": "object",
"properties": {
"attackPower": {
"type": "integer",
"minimum": 0
},
"block": {
"type": "integer",
"minimum": 0
},
"health": {
"type": "integer",
"minimum": 0
},
"defense": {
"type": "integer",
"minimum": 0
},
"evade": {
"type": "integer",
"minimum": 0
},
"attackSpeed": {
"type": "integer",
"minimum": 0
},
"parry": {
"type": "integer",
"minimum": 0
},
"stamina": {
"type": "integer",
"minimum": 0
},
"movementSpeedModifier": { "type": "integer" },
"elementalPower": { "$ref": "#/definitions/characterElements" },
"statusPower": { "$ref": "#/definitions/characterStatus" }
}
},
"attributesAndStatsContainer": {
"type": "object",
"properties": {
"attributes": { "$ref": "#/definitions/characterAttributes" },
"stats": { "$ref": "#/definitions/characterStats" }
}
},
"characterEquipmentState": {
"type": "object",
"properties": {
"weaponId": { "type": "string" },
"headgearId": { "type": "string" },
"chestId": { "type": "string" },
"legsId": { "type": "string" },
"bootsId": { "type": "string" },
"glovesId": { "type": "string" },
"firstAccessoryId": { "type": "string" },
"secondAccessoryId": { "type": "string" }
}
},
"baseObjectState": {
"type": "object",
"properties": {
"baseId": { "type": "string" },
"referenceId": { "type": "string" }
}
},
"characterState": {
"type": "object",
"allOf": [
{ "$ref": "#/definitions/baseObjectState" },
{
"properties": {
"characterName": { "type": "string" },
"characterSex": {
"type": "string",
"enum": [ "Unknown", "Male", "Female" ]
},
"race": {
"type": "string",
"enum": [ "Unknown", "Human", "Yokai" ]
},
"baseStats": { "$ref": "#/definitions/attributesAndStatsContainer" },
"currentHealth": {
"type": "integer",
"minimum": 0
},
"birthday": { "$ref": "#/definitions/gameDateTime" },
"equippedItems": { "$ref": "#/definitions/characterEquipmentState" }
}
}
]
},
"enemyStateObject": {
"type": "object",
"allOf": [
{ "$ref": "#/definitions/characterState" },
{
"properties": {
"isBoss": { "type": "boolean" }
}
}
]
},
"enemyState": {
"type": "object",
"properties": {
"enemies": {
"type": "array",
"items": { "$ref": "#/definitions/enemyStateObject" },
"minItems": 1
}
}
},
"NpcState": {
"type": "object",
"allOf": [
{ "$ref": "#/definitions/characterState" },
{
"properties": {
"backgroundInfo": { "type": "string" },
"history": { "type": "string" },
"personalityDescription": { "type": "string" },
"job": {
"type": "string",
"enum": [ "None" ]
},
"isRecruitable": { "type": "boolean" },
"isEligableSignificantOther": { "type": "boolean" },
"significantOtherId": { "type": "string" },
"wallet": {
"type": "integer",
"minimum": 0
},
"inventory": {
"type": "array",
"items": { "type": "string" }
}
}
}
]
}
}
}
I'm trying to use the enemyState definition to create an object that contains an array of enemies (doing this because the MSDN blog recommends using $schema so VS will auto-detect the desired schema).
"enemyState": {
"type": "object",
"properties": {
"enemies": {
"type": "array",
"items": { "$ref": "#/definitions/enemyStateObject" },
"minItems": 1
}
}
},
In my Enemies.json I've tried:
{
"$schema": "../../../JsonSchemas/MainSchema.json#/definitions/enemyState"
}
That's the "msdn blog post" recommended way to do it: Intellisense for JSON Schema in the JSON Editor
I've also tried omitting that, and copying the link straight into the schema location bar in the editor.
When typing the $Schema part, it is the correct location, as the #/defintions/enemyState part gets working intellisense.
However that's the extent of it. I get no intellisense or validation when trying to create the actual json. I've tried closing and re-opening the file, as well as restarting visual studio.
The desired effect I'd like to have is getting intellisense and validation so I can create an array of enemy state objects (which is the definition of various enemies in the game I'm making, and gets read and loaded into Unity) and ensure required properties are there (not yet created in the schema, figure I'd add the required properties stuff after I get it working at least), as well as making sure everything is within the ranges, and that it validates so I know there are no errors.
Any help here would be appreciated.
Thanks much!
The simplest way to check if JSON is valid is to load the JSON into a JObject or JArray and then use the IsValid(JToken, JsonSchema) method with the JSON Schema. To get validation error messages, use the IsValid(JToken, JsonSchema, IList<String> ) or Validate(JToken, JsonSchema, ValidationEventHandler) overloads.
JSON Schema is a powerful tool. It enables you to validate your JSON structure and make sure it meets the required API. You can create a schema as complex and nested as you need, all you need are the requirements. You can add it to your code as an additional test or in run-time.
JSON Schema Validation: The JSON Schema Validation specification is the document that defines the valid ways to define validation constraints. This document also defines a set of keywords that can be used to specify validations for a JSON API. In the examples that follow, we'll be using some of these keywords.
Here, we have to add the JSON body whose scheme we want to validate. Then click on the Generate Schema button. Then the corresponding scheme for the JSON gets generated at the bottom of the page. Let us create a JSON file, say schema.
Ok, so I finally got it working and here's how.
I created a test json file using Solution Explorer -> json schema and dragged that over to the JSON file, and it worked.
I then modified it to look exactly like the relevant part of my schema (tweaking the $ref's so they pointed to the main schema file where needed).
That worked.
So, for whatever reason, manually creating a json file (new -> text file -> rename) seems to throw Visual Studio off and make it think it's not a json file when being directly set as the schema in a json file (but VS is able to read it just fine, as evidenced by how it's pulling information from my main schema file, through my "test" json schema file, and on into the actual json file that's using the schema).
Go figure...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With