Packages

Native gleam json parser/generator with jsonpath querying

Current section

Files

Jump to
simplejson src simplejson.erl
Raw

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]).
-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", 39).
?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", 52).
?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", 72).
?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"
" // -> 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).