JSON
t
type t = Js.Json.t
A type representing a JSON object.
parseExn
let parseExn: string => t
parseExn(string)
Parses a JSON string or throws a JavaScript exception (SyntaxError), if the string isn't valid. It returns a JSON type.
Examples
RESCRIPTtry {
let _ = JSON.parseExn(`{"foo":"bar","hello":"world"}`)
// { foo: 'bar', hello: 'world' }
let _ = JSON.parseExn("")
// error
} catch {
| Exn.Error(obj) => Console.log("error")
}
Exceptions
Raises a SyntaxError (Exn.t) if the string isn't valid JSON.
parseExnWithReviver
let parseExnWithReviver: (string, (string, t) => t) => t
parseExnWithReviver(string, reviver)
Parses a JSON string or throws a JavaScript exception (SyntaxError), if the string isn't valid. The reviver describes how the value should be transformed. It is a function which receives a key and a value. It returns a JSON type.
Examples
RESCRIPTlet reviver = (key, value) => {
let valueType = JSON.Classify.classify(value)
switch valueType {
| String(string) => string->String.toUpperCase->JSON.Encode.string
| Number(number) => (number *. 2.0)->JSON.Encode.float
| _ => value
}
}
let jsonString = `{"hello":"world","someNumber":21}`
try {
JSON.parseExnWithReviver(jsonString, reviver)->Console.log
// { hello: 'WORLD', someNumber: 42 }
JSON.parseExnWithReviver("", reviver)->Console.log
// error
} catch {
| Exn.Error(_) => Console.log("error")
}
Exceptions
Raises a SyntaxError if the string isn't valid JSON.
stringify
let stringify: t => string
stringify(json)
Converts a JSON object to a JSON string.
If you want to stringify any type, use JSON.stringifyAny
instead.
Examples
RESCRIPTlet json =
Dict.fromArray([
("foo", JSON.Encode.string("bar")),
("hello", JSON.Encode.string("world")),
("someNumber", JSON.Encode.int(42)),
])->JSON.Encode.object
JSON.stringify(json)
// {"foo":"bar","hello":"world","someNumber":42}
stringifyWithIndent
let stringifyWithIndent: (t, int) => string
stringifyWithIndent(json, indentation)
Converts a JSON object to a JSON string. The output will be indented.
If you want to stringify any type, use JSON.stringifyAnyWithIndent
instead.
Examples
RESCRIPTlet json =
Dict.fromArray([
("foo", JSON.Encode.string("bar")),
("hello", JSON.Encode.string("world")),
("someNumber", JSON.Encode.int(42)),
])->JSON.Encode.object
JSON.stringifyWithIndent(json, 2)
// {
// "foo": "bar",
// "hello": "world",
// "someNumber": 42
// }
stringifyWithReplacer
let stringifyWithReplacer: (t, (string, t) => t) => string
stringifyWithReplacer(json, replacer)
Converts a JSON object to a JSON string.
The replacer describes how the value should be transformed. It is a function which receives a key and a value.
If you want to stringify any type, use JSON.stringifyAnyWithReplacer
instead.
Examples
RESCRIPTlet json =
Dict.fromArray([
("foo", JSON.Encode.string("bar")),
("hello", JSON.Encode.string("world")),
("someNumber", JSON.Encode.int(42)),
])->JSON.Encode.object
let replacer = (key, value) => {
let decodedValue = value->JSON.Decode.string
switch decodedValue {
| Some(string) => string->String.toUpperCase->JSON.Encode.string
| None => value
}
}
JSON.stringifyWithReplacer(json, replacer)
// {"foo":"BAR","hello":"WORLD","someNumber":42}
stringifyWithReplacerAndIndent
let stringifyWithReplacerAndIndent: (t, (string, t) => t, int) => string
stringifyWithReplacerAndIndent(json, replacer, indentation)
Converts a JSON object to a JSON string. The output will be indented.
The replacer describes how the value should be transformed. It is a function which receives a key and a value.
If you want to stringify any type, use JSON.stringifyAnyWithReplacerAndIndent
instead.
Examples
RESCRIPTlet json =
Dict.fromArray([
("foo", JSON.Encode.string("bar")),
("hello", JSON.Encode.string("world")),
("someNumber", JSON.Encode.int(42)),
])->JSON.Encode.object
let replacer = (key, value) => {
let decodedValue = value->JSON.Decode.string
switch decodedValue {
| Some(string) => string->String.toUpperCase->JSON.Encode.string
| None => value
}
}
JSON.stringifyWithReplacerAndIndent(json, replacer, 2)
// {
"foo": "BAR",
"hello": "WORLD",
"someNumber": 42
}
stringifyWithFilter
let stringifyWithFilter: (t, array<string>) => string
stringifyWithFilter(json, filter)
Converts a JSON object to a JSON string.
The filter is an array of keys, which should be included in the output.
If you want to stringify any type, use JSON.stringifyAnyWithFilter
instead.
Examples
RESCRIPTlet json =
Dict.fromArray([
("foo", JSON.Encode.string("bar")),
("hello", JSON.Encode.string("world")),
("someNumber", JSON.Encode.int(42)),
])->JSON.Encode.object
JSON.stringifyWithFilter(json, ["foo", "someNumber"])
// {"foo":"bar","someNumber":42}
stringifyWithFilterAndIndent
let stringifyWithFilterAndIndent: (t, array<string>, int) => string
stringifyWithFilterAndIndent(json, filter, indentation)
Converts a JSON object to a JSON string. The output will be indented.
The filter is an array of keys, which should be included in the output.
If you want to stringify any type, use JSON.stringifyAnyWithFilterAndIndent
instead.
Examples
RESCRIPTlet json =
Dict.fromArray([
("foo", JSON.Encode.string("bar")),
("hello", JSON.Encode.string("world")),
("someNumber", JSON.Encode.int(42)),
])->JSON.Encode.object
JSON.stringifyWithFilterAndIndent(json, ["foo", "someNumber"], 2)
// {
// "foo": "bar",
// "someNumber": 42
// }
stringifyAny
let stringifyAny: 'a => option<string>
stringifyAny(any)
Converts any type to a JSON string.
Stringifying a function or undefined
will return None
.
If the value contains circular references or BigInt
s, the function will throw a JavaScript exception (TypeError).
If you want to stringify a JSON object, use JSON.stringify
instead.
Examples
RESCRIPTlet dict = Dict.fromArray([
("foo", JSON.Encode.string("bar")),
("hello", JSON.Encode.string("world")),
("someNumber", JSON.Encode.int(42)),
])
JSON.stringifyAny(dict)
// {"foo":"bar","hello":"world","someNumber":42}
JSON.stringifyAny(() => "hello world")
// None
BigInt.fromInt(0)->JSON.stringifyAny
// exception
Exceptions
Raises a TypeError if the value contains circular references.
Raises a TypeError if the value contains
BigInt
s.
stringifyAnyWithIndent
let stringifyAnyWithIndent: ('a, int) => option<string>
stringifyAnyWithIndent(any, indentation)
Converts any type to a JSON string. The output will be indented.
Stringifying a function or undefined
will return None
.
If the value contains circular references or BigInt
s, the function will throw a JavaScript exception (TypeError).
If you want to stringify a JSON object, use JSON.stringifyWithIndent
instead.
Examples
RESCRIPTlet dict = Dict.fromArray([
("foo", JSON.Encode.string("bar")),
("hello", JSON.Encode.string("world")),
("someNumber", JSON.Encode.int(42)),
])
JSON.stringifyAnyWithIndent(dict, 2)
// {
// "foo": "bar",
// "hello": "world",
// "someNumber": 42
// }
JSON.stringifyAny(() => "hello world")
// None
BigInt.fromInt(0)->JSON.stringifyAny
// exception
Exceptions
Raises a TypeError if the value contains circular references.
Raises a TypeError if the value contains
BigInt
s.
stringifyAnyWithReplacer
let stringifyAnyWithReplacer: ('a, (string, t) => t) => option<string>
stringifyAnyWithReplacer(json, replacer)
Converts any type to a JSON string.
The replacer describes how the value should be transformed. It is a function which receives a key and a value.
Stringifying a function or undefined
will return None
.
If the value contains circular references or BigInt
s, the function will throw a JavaScript exception (TypeError).
If you want to stringify a JSON object, use JSON.stringifyWithReplacer
instead.
Examples
RESCRIPTlet dict = Dict.fromArray([
("foo", JSON.Encode.string("bar")),
("hello", JSON.Encode.string("world")),
("someNumber", JSON.Encode.int(42)),
])
let replacer = (key, value) => {
let decodedValue = value->JSON.Decode.string
switch decodedValue {
| Some(string) => string->String.toUpperCase->JSON.Encode.string
| None => value
}
}
JSON.stringifyAnyWithReplacer(dict, replacer)
// {"foo":"BAR","hello":"WORLD","someNumber":42}
JSON.stringifyAny(() => "hello world")
// None
BigInt.fromInt(0)->JSON.stringifyAny
// exception
Exceptions
Raises a TypeError if the value contains circular references.
Raises a TypeError if the value contains
BigInt
s.
stringifyAnyWithReplacerAndIndent
let stringifyAnyWithReplacerAndIndent: ('a, (string, t) => t, int) => option<string>
stringifyAnyWithReplacerAndIndent(json, replacer, indentation)
Converts any type to a JSON string. The output will be indented.
The replacer describes how the value should be transformed. It is a function which receives a key and a value.
Stringifying a function or undefined
will return None
.
If the value contains circular references or BigInt
s, the function will throw a JavaScript exception (TypeError).
If you want to stringify a JSON object, use JSON.stringifyWithReplacerAndIndent
instead.
Examples
RESCRIPTlet dict = Dict.fromArray([
("foo", JSON.Encode.string("bar")),
("hello", JSON.Encode.string("world")),
("someNumber", JSON.Encode.int(42)),
])
let replacer = (key, value) => {
let decodedValue = value->JSON.Decode.string
switch decodedValue {
| Some(string) => string->String.toUpperCase->JSON.Encode.string
| None => value
}
}
JSON.stringifyAnyWithReplacerAndIndent(dict, replacer, 2)
// {
// "foo": "BAR",
// "hello": "WORLD",
// "someNumber": 42
// }
JSON.stringifyAny(() => "hello world")
// None
BigInt.fromInt(0)->JSON.stringifyAny
// exception
Exceptions
Raises a TypeError if the value contains circular references.
Raises a TypeError if the value contains
BigInt
s.
stringifyAnyWithFilter
let stringifyAnyWithFilter: ('a, array<string>) => string
stringifyAnyWithFilter(json, filter)
Converts any type to a JSON string.
The filter is an array of keys, which should be included in the output.
Stringifying a function or undefined
will return None
.
If the value contains circular references or BigInt
s, the function will throw a JavaScript exception (TypeError).
If you want to stringify a JSON object, use JSON.stringifyWithFilter
instead.
Examples
RESCRIPTlet dict = Dict.fromArray([
("foo", JSON.Encode.string("bar")),
("hello", JSON.Encode.string("world")),
("someNumber", JSON.Encode.int(42)),
])
JSON.stringifyAnyWithFilter(dict, ["foo", "someNumber"])
// {"foo": "bar","someNumber": 42}
JSON.stringifyAny(() => "hello world")
// None
BigInt.fromInt(0)->JSON.stringifyAny
// exception
Exceptions
Raises a TypeError if the value contains circular references.
Raises a TypeError if the value contains
BigInt
s.
stringifyAnyWithFilterAndIndent
let stringifyAnyWithFilterAndIndent: ('a, array<string>, int) => string
stringifyAnyWithFilterAndIndent(json, filter, indentation)
Converts any type to a JSON string. The output will be indented.
The filter is an array of keys, which should be included in the output.
Stringifying a function or undefined
will return None
.
If the value contains circular references or BigInt
s, the function will throw a JavaScript exception (TypeError).
If you want to stringify a JSON object, use JSON.stringifyWithFilterAndIndent
instead.
Examples
RESCRIPTlet dict = Dict.fromArray([
("foo", JSON.Encode.string("bar")),
("hello", JSON.Encode.string("world")),
("someNumber", JSON.Encode.int(42)),
])
JSON.stringifyAnyWithFilterAndIndent(dict, ["foo", "someNumber"], 2)
// {
// "foo": "bar",
// "someNumber": 42
// }
JSON.stringifyAny(() => "hello world")
// None
BigInt.fromInt(0)->JSON.stringifyAny
// exception
Exceptions
Raises a TypeError if the value contains circular references.
Raises a TypeError if the value contains
BigInt
s.