Current section
Files
Jump to
Current section
Files
src/simplejson.erl
-module(simplejson).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/simplejson.gleam").
-export([parse/1, to_string/1, jsonpath/2, to_path/1, 'query'/2, apply_pointer/2]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(
" SimpleJSON\n"
"\n"
" Basic JSON library for Gleam.\n"
" To be used for simple conversion from string to a basic JSON structure\n"
" and to then output that as a string again.\n"
).
-file("src/simplejson.gleam", 42).
?DOC(
" Parse a given string into a JsonValue Result.\n"
" Or return Error if unable.\n"
"\n"
" Thie returns a useful description\n"
" of the parse failure utilising the `ParseError` types.\n"
"\n"
" The error will either be `UnexpectedEnd` or a specific reason\n"
" containing the character/value that failed to parse, a context which\n"
" contains the surrounding up to 10 characters and the character\n"
" index of the failure point\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" parse(\"{\\\"a\\\":123,\\\"b\\\":[true, false]}\")\n"
" // -> Ok(JsonObject(dict.from_list([#(\"a\", JsonNumber(Some(123), None, Some(\"123\"))), #(\"b\", JsonArray([JsonBool(True), JsonBool(False)]))])))\n"
" ```\n"
"\n"
" ```gleam\n"
" parse(\"[1,2,3]\")\n"
" // -> Ok(JsonArray([JsonNumber(Some(1), None, Some(\"1\")), JsonNumber(Some(2), None, Some(\"2\")), JsonNumber(Some(3), None, Some(\"3\"))]))\n"
" ```\n"
"\n"
" ```gleam\n"
" parse(\"[1,2,3,]\")\n"
" // -> Error(UnexpectedCharacter(\"]\", \",2,3,]\", 7))\n"
" ```\n"
).
-spec parse(binary()) -> {ok, simplejson@jsonvalue:json_value()} |
{error, simplejson@jsonvalue:parse_error()}.
parse(Json) ->
simplejson@internal@parser:parse(Json).
-file("src/simplejson.gleam", 55).
?DOC(
" Convert a given JsonValue into a String\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" to_string(JsonArray([JsonNumber(Some(1), None, Some(\"1\")), JsonNumber(Some(2), None, Some(\"2\")), JsonNumber(Some(3), None, Some(\"3\"))]))\n"
" // -> \"[1,2,3]\"\n"
" ```\n"
).
-spec to_string(simplejson@jsonvalue:json_value()) -> binary().
to_string(Json) ->
simplejson@internal@stringify:to_string(Json).
-file("src/simplejson.gleam", 75).
?DOC(
" Simple jsonpath style querying method\n"
"\n"
" A simple **.** separated list of path elements to take\n"
" - names are as-is\n"
" - indexes are prefixes by #\n"
" - consecutive separators are ignored\n"
" - e.g. key1.#3...nextkey\n"
"\n"
"\n"
" ## Examples\n"
"\n"
" ```Gleam\n"
" let assert Ok(json) = simplejson.parse(\"{\\\"a\\\":[1,2,{\\\"b\\\":123}]}\")\n"
" simplejson.jsonpath(json, \"a.#2.b\")\n"
" // -> Ok(JsonNumber(Some(123), None, Some(\"123\")))\n"
" ```\n"
).
-spec jsonpath(simplejson@jsonvalue:json_value(), binary()) -> {ok,
simplejson@jsonvalue:json_value()} |
{error, simplejson@jsonvalue:json_path_error()}.
jsonpath(Json, Jsonpath) ->
simplejson@internal@pointer:jsonpath(Json, Jsonpath).
-file("src/simplejson.gleam", 92).
?DOC(
" Converts the passed string into a query type to be used in the query function\n"
"\n"
" This parses based on RFC9535 (https://www.rfc-editor.org/rfc/rfc9535)\n"
"\n"
" ## Examples\n"
"\n"
" ```Gleam\n"
" let assert Ok(path) = simplejson.to_path(\"$[1]\")\n"
" // -> [Child([Index(1)])]\n"
" ```\n"
).
-spec to_path(binary()) -> {ok, list(simplejson@internal@jsonpath:segment())} |
{error, simplejson@jsonvalue:json_path_error()}.
to_path(Str) ->
simplejson@internal@jsonpath:parse_path(Str).
-file("src/simplejson.gleam", 108).
?DOC(
" Takes the provided path and json and returns a Json Array of results\n"
"\n"
" This executes based on RFC9535 (https://www.rfc-editor.org/rfc/rfc9535)\n"
"\n"
" ## Examples\n"
"\n"
" ```Gleam\n"
" let assert Ok(path) = simplejson.to_path(\"$[1]\")\n"
" let assert Ok(json) = simplejson.parse(\"[1,2,3]\")\n"
" simplejson.to_string(simplejson.query(json, path))\n"
" // -> [2]\n"
" ```\n"
).
-spec 'query'(
simplejson@jsonvalue:json_value(),
list(simplejson@internal@jsonpath:segment())
) -> simplejson@jsonvalue:json_value().
'query'(Json, Path) ->
simplejson@internal@query:'query'(Json, Path, Json).
-file("src/simplejson.gleam", 124).
?DOC(
" Takes the provided JSONPointer and returns the JsonValue that the pointer refers to\n"
" or Error(Nil) if it fails for some reason\n"
"\n"
" This executes based on RFC6901 (https://www.rfc-editor.org/rfc/rfc6901)\n"
"\n"
" ## Examples\n"
"\n"
" ```Gleam\n"
" let assert Ok(json) = simplejson.parse(\"{\\\"a\\\":[1,{\\\"b\\\":2},3]}\")\n"
" simplejson.apply_pointer(json, \"/a/1/b\")\n"
" // -> Ok(JsonNumber(Some(2), None, Some(\"2\")))\n"
" ```\n"
).
-spec apply_pointer(simplejson@jsonvalue:json_value(), binary()) -> {ok,
simplejson@jsonvalue:json_value()} |
{error, nil}.
apply_pointer(Json, Pointer) ->
_pipe = simplejson@internal@pointer:jsonpointer(Json, Pointer),
gleam@result:replace_error(_pipe, nil).