JSONSchema =========== This is an implementation of JSON Schema for the pharo language. It is used to define the structure and values of a JSON string and to validate it. The schema itself can be externalized for being consumed by a third party. I like to announce the availability of a JSON schema implementation for pharo. As part of my implementation of OpenAPI (which is to be released a bit later) I factored out the JSON schema part into its own repository because I think it is useful. I release it even it is not really finished. Code is mostly undocumented and a lot of features are missing from the full spec. I will improve it slowly and add features as I need them or they being requested Hope you like it! Norbert ==== The documentation so far (from https://github.com/zweidenker/JSONSchema) It can be loaded by downloading it in pharo via Metacello new repository: '<a href="github://zweidenker/JSONSchema" class="">github://zweidenker/JSONSchema'; baseline: #JSONSchema; load Defining a schema ------------------------- These are the expression to create a schema model inside pharo. schema := { #name -> JSONSchema string. #dateAndTime -> (JSONSchema stringWithFormat: 'date-time'). #numberOfPets -> JSONSchema number } asJSONSchema. defines as schema that can parse the following JSON: jsonString := '{ "name" : "John Doe", "dateAndTime" : "1970-01-01T14:00:00", "numberOfPets" : 3 }'. Reading/Writing a value using a schema ------------------------------------------------------ To parse the value from JSON we only need to invoke: value := schema read: jsonString The object in value will have name as a string, dateAndTime as a DateAndTime object and numberOfPets as a SmallInteger object. The schema can also be used to write out the value as JSON. This is especially useful if we want to ensure that only valid JSON is written. For this invoke jsonString := schema write: value. Serialize/Materialize a schema ---------------------------------------- Addtionally to reading and writing objects a schema can be serialized to string. schemaString := NeoJSONWriter toStringPretty: schema. gives { "type" : "object", "properties" : { "name" : { "type" : "string" }, "numberOfPets" : { "type" : "number" }, "dateAndTime" : { "type" : "string", "format" : "date-time" } } } If we would get a schema as string we can instantiate by invoking schema := JSONSchema fromString: schemaString. Nested schemas ----------------------- Schemas can be nested in any depth. And it can be specified by using the literal Array syntax. schema := { #name -> JSONSchema string. #address -> { #street -> JSONSchema string. #number -> JSONSchema number } } asJSONSchema Constraints --------------- JSON Schema has a defined set of constraints that can be specified. E.g. for a number the inerval of the value can be specified by numberSchema := JSONSchema number. numberSchema interval minimum: 1; exclusiveMaximum: 100 constraining the number value to be greater or equal to 1 and smaller than 100. |
Nice. Another piece of the puzzle. Thank you.
> On 22 Nov 2018, at 17:56, Norbert Hartl <[hidden email]> wrote: > > JSONSchema > =========== > > This is an implementation of JSON Schema for the pharo language. It is used to define the structure and values of a JSON string and to validate it. The schema itself can be externalized for being consumed by a third party. > > I like to announce the availability of a JSON schema implementation for pharo. As part of my implementation of OpenAPI (which is to be released a bit later) I factored out the JSON schema part into its own repository because I think it is useful. I release it even it is not really finished. Code is mostly undocumented and a lot of features are missing from the full spec. I will improve it slowly and add features as I need them or they being requested > > Hope you like it! > > Norbert > > ==== > > The documentation so far (from https://github.com/zweidenker/JSONSchema) > > It can be loaded by downloading it in pharo via > > Metacello new > repository: 'github://zweidenker/JSONSchema'; > baseline: #JSONSchema; > load > > Defining a schema > ------------------------- > > These are the expression to create a schema model inside pharo. > > schema := { > #name -> JSONSchema string. > #dateAndTime -> (JSONSchema stringWithFormat: 'date-time'). > #numberOfPets -> JSONSchema number } asJSONSchema. > > > defines as schema that can parse the following JSON: > > jsonString := '{ > "name" : "John Doe", > "dateAndTime" : "1970-01-01T14:00:00", > "numberOfPets" : 3 > }'. > > Reading/Writing a value using a schema > ------------------------------------------------------ > > To parse the value from JSON we only need to invoke: > > value := schema read: jsonString > > The object in value will have name as a string, dateAndTime as a DateAndTime object and numberOfPets as a SmallInteger object. > > The schema can also be used to write out the value as JSON. This is especially useful if we want to ensure that only valid JSON is written. For this invoke > > jsonString := schema write: value. > > Serialize/Materialize a schema > ---------------------------------------- > > Addtionally to reading and writing objects a schema can be serialized to string. > > schemaString := NeoJSONWriter toStringPretty: schema. > > gives > > { > "type" : "object", > "properties" : { > "name" : { > "type" : "string" > }, > "numberOfPets" : { > "type" : "number" > }, > "dateAndTime" : { > "type" : "string", > "format" : "date-time" > } > } > } > > > If we would get a schema as string we can instantiate by invoking > > schema := JSONSchema fromString: schemaString. > > Nested schemas > ----------------------- > > Schemas can be nested in any depth. And it can be specified by using the literal Array syntax. > > schema := { > #name -> JSONSchema string. > #address -> { > #street -> JSONSchema string. > #number -> JSONSchema number > } } asJSONSchema > > Constraints > --------------- > > JSON Schema has a defined set of constraints that can be specified. E.g. for a number the inerval of the value can be specified by > > numberSchema := JSONSchema number. > numberSchema interval > minimum: 1; > exclusiveMaximum: 100 > > constraining the number value to be greater or equal to 1 and smaller than 100. > |
This is great. Thanks! On Thu, 22 Nov 2018 at 18:10 Sven Van Caekenberghe <[hidden email]> wrote: Nice. Another piece of the puzzle. Thank you. Sent from the past
|
In reply to this post by Sven Van Caekenberghe-2
Thank you Norbert and IID.
Another brick in the bridge towards mainstream. :) Regards, El jue., 22 nov. 2018 a las 18:11, Sven Van Caekenberghe (<[hidden email]>) escribió: > > Nice. Another piece of the puzzle. Thank you. > > > On 22 Nov 2018, at 17:56, Norbert Hartl <[hidden email]> wrote: > > > > JSONSchema > > =========== > > > > This is an implementation of JSON Schema for the pharo language. It is used to define the structure and values of a JSON string and to validate it. The schema itself can be externalized for being consumed by a third party. > > > > I like to announce the availability of a JSON schema implementation for pharo. As part of my implementation of OpenAPI (which is to be released a bit later) I factored out the JSON schema part into its own repository because I think it is useful. I release it even it is not really finished. Code is mostly undocumented and a lot of features are missing from the full spec. I will improve it slowly and add features as I need them or they being requested > > > > Hope you like it! > > > > Norbert > > > > ==== > > > > The documentation so far (from https://github.com/zweidenker/JSONSchema) > > > > It can be loaded by downloading it in pharo via > > > > Metacello new > > repository: 'github://zweidenker/JSONSchema'; > > baseline: #JSONSchema; > > load > > > > Defining a schema > > ------------------------- > > > > These are the expression to create a schema model inside pharo. > > > > schema := { > > #name -> JSONSchema string. > > #dateAndTime -> (JSONSchema stringWithFormat: 'date-time'). > > #numberOfPets -> JSONSchema number } asJSONSchema. > > > > > > defines as schema that can parse the following JSON: > > > > jsonString := '{ > > "name" : "John Doe", > > "dateAndTime" : "1970-01-01T14:00:00", > > "numberOfPets" : 3 > > }'. > > > > Reading/Writing a value using a schema > > ------------------------------------------------------ > > > > To parse the value from JSON we only need to invoke: > > > > value := schema read: jsonString > > > > The object in value will have name as a string, dateAndTime as a DateAndTime object and numberOfPets as a SmallInteger object. > > > > The schema can also be used to write out the value as JSON. This is especially useful if we want to ensure that only valid JSON is written. For this invoke > > > > jsonString := schema write: value. > > > > Serialize/Materialize a schema > > ---------------------------------------- > > > > Addtionally to reading and writing objects a schema can be serialized to string. > > > > schemaString := NeoJSONWriter toStringPretty: schema. > > > > gives > > > > { > > "type" : "object", > > "properties" : { > > "name" : { > > "type" : "string" > > }, > > "numberOfPets" : { > > "type" : "number" > > }, > > "dateAndTime" : { > > "type" : "string", > > "format" : "date-time" > > } > > } > > } > > > > > > If we would get a schema as string we can instantiate by invoking > > > > schema := JSONSchema fromString: schemaString. > > > > Nested schemas > > ----------------------- > > > > Schemas can be nested in any depth. And it can be specified by using the literal Array syntax. > > > > schema := { > > #name -> JSONSchema string. > > #address -> { > > #street -> JSONSchema string. > > #number -> JSONSchema number > > } } asJSONSchema > > > > Constraints > > --------------- > > > > JSON Schema has a defined set of constraints that can be specified. E.g. for a number the inerval of the value can be specified by > > > > numberSchema := JSONSchema number. > > numberSchema interval > > minimum: 1; > > exclusiveMaximum: 100 > > > > constraining the number value to be greater or equal to 1 and smaller than 100. > > > > |
In reply to this post by NorbertHartl
Nice work! Thanks!
Doru > On Nov 22, 2018, at 5:56 PM, Norbert Hartl <[hidden email]> wrote: > > JSONSchema > =========== > > This is an implementation of JSON Schema for the pharo language. It is used to define the structure and values of a JSON string and to validate it. The schema itself can be externalized for being consumed by a third party. > > I like to announce the availability of a JSON schema implementation for pharo. As part of my implementation of OpenAPI (which is to be released a bit later) I factored out the JSON schema part into its own repository because I think it is useful. I release it even it is not really finished. Code is mostly undocumented and a lot of features are missing from the full spec. I will improve it slowly and add features as I need them or they being requested > > Hope you like it! > > Norbert > > ==== > > The documentation so far (from https://github.com/zweidenker/JSONSchema) > > It can be loaded by downloading it in pharo via > > Metacello new > repository: 'github://zweidenker/JSONSchema'; > baseline: #JSONSchema; > load > > Defining a schema > ------------------------- > > These are the expression to create a schema model inside pharo. > > schema := { > #name -> JSONSchema string. > #dateAndTime -> (JSONSchema stringWithFormat: 'date-time'). > #numberOfPets -> JSONSchema number } asJSONSchema. > > > defines as schema that can parse the following JSON: > > jsonString := '{ > "name" : "John Doe", > "dateAndTime" : "1970-01-01T14:00:00", > "numberOfPets" : 3 > }'. > > Reading/Writing a value using a schema > ------------------------------------------------------ > > To parse the value from JSON we only need to invoke: > > value := schema read: jsonString > > The object in value will have name as a string, dateAndTime as a DateAndTime object and numberOfPets as a SmallInteger object. > > The schema can also be used to write out the value as JSON. This is especially useful if we want to ensure that only valid JSON is written. For this invoke > > jsonString := schema write: value. > > Serialize/Materialize a schema > ---------------------------------------- > > Addtionally to reading and writing objects a schema can be serialized to string. > > schemaString := NeoJSONWriter toStringPretty: schema. > > gives > > { > "type" : "object", > "properties" : { > "name" : { > "type" : "string" > }, > "numberOfPets" : { > "type" : "number" > }, > "dateAndTime" : { > "type" : "string", > "format" : "date-time" > } > } > } > > > If we would get a schema as string we can instantiate by invoking > > schema := JSONSchema fromString: schemaString. > > Nested schemas > ----------------------- > > Schemas can be nested in any depth. And it can be specified by using the literal Array syntax. > > schema := { > #name -> JSONSchema string. > #address -> { > #street -> JSONSchema string. > #number -> JSONSchema number > } } asJSONSchema > > Constraints > --------------- > > JSON Schema has a defined set of constraints that can be specified. E.g. for a number the inerval of the value can be specified by > > numberSchema := JSONSchema number. > numberSchema interval > minimum: 1; > exclusiveMaximum: 100 > > constraining the number value to be greater or equal to 1 and smaller than 100. > -- www.feenk.com "What is more important: To be happy, or to make happy?" |
Free forum by Nabble | Edit this page |