Header

JSON-dataformat

JSON (JavaScript Object Notation) is the universal language of data. It is used everywhere in the internet as it is the native structure in JavaScript, which is the language used in the webpages. Most modern APIs are built to support primary JSON. JSON is concise and nice to read, both by humans and machines. In this section we will look more closely into JSON and also see how Operon extends it. To start, JSON is used to describe data. There are the following basic data types available:

  • String: "this is a string"
  • Number: 1, 3, -10, 3.14159
  • Array: [1, 2, 3, "four", null, true, false, {"foo": "bar"}]
  • Object: {"fname": "Foo", "lname": "Bar", "age": 24}
  • Boolean: true, false
  • Null: null

Operon adds the following types:

  • Empty: empty
  • Binary: Binary-value.
  • Stream: Stream-value.

Each value in Operon has some of the above data-types. Types do not need to be explicitly told, as they are implicit in the value itself. However, it is possible to tell the type:

From json:empty
Let $myString <String>: "Hello" End
Select $myString

#> "Hello"

In the query above we tell that the $myString is bound with value "Hello", and this value is String. If we try to assign e.g. a number, then we will get an error.

Type-system

The type-system in Operon is based on JSON-data types and in the use of core:mappableTo(...) and core:type() -functions. This works by restricting the domain of values the target value can have. Without restrictions, the value can assume any JSON-value at runtime. However, we can say e.g. that value can only be String-type, which restricts the type from known JSON-types only to String. We can further say that, the value must be StringType, and the maximum length must be 10 characters. This can be allowed be the following expression:

From json:empty
Let $myString <String And => string:length() <= 10>: "Hello" End
Select $myString

#> "Hello"

If we try to define string that is longer than 10 characters then Operon will throw an exception and report error. The String is a shortcut of expression => type() = "String", which evaluates to Boolean (true or false).

While it is unnecessary to explicitly tell the typename in the Let -statement example above, it will become handy when we are assigning value from e.g. integration-call. In this case we do not know beforehand that the system we are integrating data from will always return the correct value-type. If the value does not match the restriction, then an error is risen.

Union types

Union-type means, that the value can assume multiple type, depending on context. E.g. we can define that value can be either String or Number, by the restriction given in the example below:

Function process($input <String Or Number>):
  $input + 100
End

Proceed next to: Query-structure



Backlinks:
Operon.io:Introduction