Ludwig.JSON

Module Members

check

(Function)

Check if the given String is valid JSON.

Example:

x: JSON.check('{"key": 123}') => Validation.Success
y: JSON.check('{"key: 123}') => Validation.Failure("invalid JSON")

Type Signature

 fun (String) -> Validation
Argument: s
Type: String
Returns:
Type: Validation

get

(Function)

Retrieve a member of an Object. Throws an error if JSON value is not an Object or the key doesn’t exist.

Example:

j: Optional.unpackOrError(JSON.parse("{\"value\": 123}"), "invalid JSON") # => # JSON.Object({"value": JSON.Integer(123)})
x: JSON.get("value", j)   # => JSON.Integer(123)
y: JSON.get("value_2", j) # => error

Type Signature

 fun (String, JSON) -> JSON
Argument: k
Type: String
Argument: j
Type: JSON
Returns:
Type: JSON

index

(Function)

Retrieve a List element by index. Throws an error if JSON is not a List.

Example:

x: Optional.unpackOrError(JSON.parse("[true,false,123.45]"), "invalid JSON")
y: JSON.toFloat(JSON.index(x, 2)) # => 123.45

Type Signature

 fun (JSON, Int) -> JSON
Argument: j
Type: JSON
Argument: i
Type: Int
Returns:
Type: JSON

key

(Function)

DEPRECATED: Use get instead.

Retrieve a member of an Object. Throws an error if JSON value is not an Object or the key doesn’t exist.

Example:

j: JSON.Object {"value": JSON.Integer(123)}
x: JSON.get("value", j)    # => JSON.Integer(123)
y: JSON.get("value_2", j)  # => error

Type Signature

 fun (String, JSON) -> JSON
Argument: k
Type: String
Argument: j
Type: JSON
Returns:
Type: JSON

keyOptional

(Function)

DEPRECATED: Use lookup instead.

Like key, but returns None instead of throwing an error.

Type Signature

 fun (String, JSON) -> Optional<JSON>
Argument: k
Type: String
Argument: j
Type: JSON
Returns:
Type: Optional<JSON>

keys

(Function)

If JSON is an Object return the keys. Otherwise throw an error.

Type Signature

 fun (JSON) -> List<String>
Argument: j
Type: JSON
Returns:
Type: List<String>

lookup

(Function)

Like get but returns None instead of throwing an error.

Type Signature

 fun (String, JSON) -> Optional<JSON>
Argument: k
Type: String
Argument: j
Type: JSON
Returns:
Type: Optional<JSON>

member

(Function)

Check if a key exists in JSON Object. Throws an error if JSON value is not an Object.

Type Signature

 fun (String, JSON) -> Bool
Argument: k
Type: String
Argument: j
Type: JSON
Returns:
Type: Bool

parse

(Function)

Parse a String into a structured JSON type.

Example

j: Optional.unpackOrError(JSON.parse("{\"value\": 123}"), "invalid JSON") # => # JSON.Object({"value": JSON.Integer(123)})

Type Signature

 fun (String) -> Optional<JSON>
Argument: s
Type: String
Returns:
Type: Optional<JSON>

stringify

(Function)

Convert a structured JSON value into a String

Example:

JSON.stringify(JSON.Object({"value": JSON.Integer(123)})) # => "{\"value\":123}"

Type Signature

 fun (JSON) -> String
Argument: j
Type: JSON
Returns:
Type: String

toBool

(Function)

Convert a JSON value into a Bool value. Throws an error if JSON is not a Bool.

Example:

x: Optional.unpackOrError(JSON.parse("[true,false]"), "invalid JSON")
y: List.map(JSON.toBool, JSON.toList(x)) # => [True, False]

Type Signature

 fun (JSON) -> Bool
Argument: j
Type: JSON
Returns:
Type: Bool

toBoolOptional

(Function)

Like toBool but returns None instead of throwing an error.

Type Signature

 fun (JSON) -> Optional<Bool>
Argument: j
Type: JSON
Returns:
Type: Optional<Bool>

toFloat

(Function)

Convert a JSON value into a Float value. Note: both Ints and Floats are accepted. Throws an error if JSON is not a Float or Int.

Example:

x: Optional.unpackOrError(JSON.parse("[123.45, 6]"), "invalid JSON")
List<Float> y: List.map(JSON.toFloat, JSON.toList(x)) # => [123.45, 6]

Type Signature

 fun (JSON) -> Float
Argument: j
Type: JSON
Returns:
Type: Float

toFloatOptional

(Function)

Like toFloat but returns None instead of throwing an error.

Type Signature

 fun (JSON) -> Optional<Float>
Argument: j
Type: JSON
Returns:
Type: Optional<Float>

toInt

(Function)

Convert a JSON value into an Int value. Throws an error if JSON is not an Int. This includes Floats.

Example:

x: Optional.unpackOrError(JSON.parse("123"), "invalid JSON")
y: JSON.toInt(x) # => 123

Type Signature

 fun (JSON) -> Int
Argument: j
Type: JSON
Returns:
Type: Int

toIntOptional

(Function)

Like toInt but returns None instead of throwing an error.

Example:

x: Optional.unpackOrError(JSON.parse("\"string\""), "invalid JSON")
y: JSON.toString(x) # => "string"

Type Signature

 fun (JSON) -> Optional<Int>
Argument: j
Type: JSON
Returns:
Type: Optional<Int>

toList

(Function)

Convert a JSON value into a List of JSON values. Throws an error if JSON is not a List.

Example:

x: Optional.unpackOrError(JSON.parse("[true,false,123.45]"), "invalid JSON")
y: JSON.toList(x) # => [Bool(True), Bool(False), Float(123.45)]

Type Signature

 fun (JSON) -> List<JSON>
Argument: j
Type: JSON
Returns:
Type: List<JSON>

toString

(Function)

Convert a JSON value into a String value. Throws an error if JSON is not a String.

Type Signature

 fun (JSON) -> String
Argument: j
Type: JSON
Returns:
Type: String

toStringOptional

(Function)

Like toString but returns None instead of throwing an error.

Type Signature

 fun (JSON) -> Optional<String>
Argument: j
Type: JSON
Returns:
Type: Optional<String>

values

(Function)

If JSON is an Object return the values. Otherwise throw an error.

Type Signature

 fun (JSON) -> List<JSON>
Argument: j
Type: JSON
Returns:
Type: List<JSON>

JSON

(Type)

type JSON:
  | Object Dictionary<JSON>
  | Array List<JSON>
  | String String
  | Number Float
  | Integer Int
  | Bool Bool
  | Null

Array

(Constructor)

Argument:
Ludwig.List<JSON>

Bool

(Constructor)

Argument:
Ludwig.Bool

Integer

(Constructor)

Argument:
Ludwig.Int

Null

(Constructor)

Number

(Constructor)

Argument:
Ludwig.Float

Object

(Constructor)

Argument:
Ludwig.Dictionary<JSON>

String

(Constructor)

Argument:
Ludwig.String