Packages

Convert between XML and JSON using the BadgerFish specification

Current section

Files

Jump to
xmljson src xmljson@internal@json.erl
Raw

src/xmljson@internal@json.erl

-module(xmljson@internal@json).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([namespace_to_xml/1, parse/1]).
-file("/home/edwin/Documents/code/gleam/badgerfish/src/xmljson/internal/json.gleam", 93).
-spec namespace_to_xml({binary(), simplejson@jsonvalue:json_value()}) -> list({binary(),
binary()}).
namespace_to_xml(Value) ->
{<<"@xmlns"/utf8>>, {json_object, Dct}} = case Value of
{<<"@xmlns"/utf8>>, {json_object, _}} -> Value;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"xmljson/internal/json"/utf8>>,
function => <<"namespace_to_xml"/utf8>>,
line => 96})
end,
_pipe = Dct,
_pipe@1 = maps:to_list(_pipe),
gleam@list:map(
_pipe@1,
fun(Pair) ->
{Key, {json_string, Nm}} = case Pair of
{_, {json_string, _}} -> Pair;
_assert_fail@1 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail@1,
module => <<"xmljson/internal/json"/utf8>>,
function => <<"namespace_to_xml"/utf8>>,
line => 100})
end,
case Key of
<<"$"/utf8>> ->
{<<"xmlns"/utf8>>, Nm};
Value@1 ->
{<<"xmlns:"/utf8, Value@1/binary>>, Nm}
end
end
).
-file("/home/edwin/Documents/code/gleam/badgerfish/src/xmljson/internal/json.gleam", 21).
-spec parse_json(
list({binary(), simplejson@jsonvalue:json_value()}),
list({binary(), binary()})
) -> list(xmb:xml()).
parse_json(Elements, Namespaces) ->
_pipe = Elements,
_pipe@2 = gleam@list:flat_map(_pipe, fun(Pair) -> case Pair of
{Key, {json_array, Arr}} ->
_pipe@1 = Arr,
gleam@list:map(_pipe@1, fun(Value) -> {Key, Value} end);
_ ->
[Pair]
end end),
gleam@list:map(
_pipe@2,
fun(Pair@1) ->
{Key@1, Value@1} = Pair@1,
case Key@1 of
<<"$"/utf8>> ->
{json_string, Value@2} = case Value@1 of
{json_string, _} -> Value@1;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"xmljson/internal/json"/utf8>>,
function => <<"parse_json"/utf8>>,
line => 37})
end,
xmb:text(Value@2);
Key@2 ->
{json_object, Dct} = case Value@1 of
{json_object, _} -> Value@1;
_assert_fail@1 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail@1,
module => <<"xmljson/internal/json"/utf8>>,
function => <<"parse_json"/utf8>>,
line => 43})
end,
Arr@1 = maps:to_list(Dct),
Dct@1 = begin
_pipe@3 = Arr@1,
gleam@list:group(_pipe@3, fun(Pair@2) -> case Pair@2 of
{<<"@xmlns"/utf8>>, _} ->
<<"namespace"/utf8>>;
{<<"@"/utf8, _/binary>>, _} ->
<<"attribute"/utf8>>;
_ ->
<<"element"/utf8>>
end end)
end,
Elements@1 = begin
_pipe@4 = gleam_stdlib:map_get(
Dct@1,
<<"element"/utf8>>
),
_pipe@5 = gleam@result:unwrap(_pipe@4, []),
lists:reverse(_pipe@5)
end,
Attributes = begin
_pipe@6 = gleam_stdlib:map_get(
Dct@1,
<<"attribute"/utf8>>
),
_pipe@7 = gleam@result:unwrap(_pipe@6, []),
gleam@list:map(
_pipe@7,
fun(Pair@3) ->
{<<"@"/utf8, Key@3/binary>>,
{json_string, Value@3}} = case Pair@3 of
{<<"@"/utf8, _/binary>>, {json_string, _}} -> Pair@3;
_assert_fail@2 ->
erlang:error(
#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail@2,
module => <<"xmljson/internal/json"/utf8>>,
function => <<"parse_json"/utf8>>,
line => 65}
)
end,
{Key@3, Value@3}
end
)
end,
Current_namespaces = begin
_pipe@8 = gleam_stdlib:map_get(
Dct@1,
<<"namespace"/utf8>>
),
_pipe@9 = gleam@result:unwrap(_pipe@8, []),
_pipe@10 = gleam@list:map(
_pipe@9,
fun(_capture) -> namespace_to_xml(_capture) end
),
gleam@list:flatten(_pipe@10)
end,
Dct@2 = maps:from_list(Namespaces),
New_namespaces = gleam@list:filter(
Current_namespaces,
fun(Pair@4) ->
{K, _} = Pair@4,
not gleam@dict:has_key(Dct@2, K)
end
),
xmb:x(
Key@2,
lists:append(New_namespaces, Attributes),
parse_json(
Elements@1,
lists:append(Namespaces, New_namespaces)
)
)
end
end
).
-file("/home/edwin/Documents/code/gleam/badgerfish/src/xmljson/internal/json.gleam", 8).
-spec parse(simplejson@jsonvalue:json_value()) -> list(xmb:xml()).
parse(Input) ->
case Input of
{json_object, Input@1} ->
Arr = maps:to_list(Input@1),
parse_json(Arr, []);
Value ->
gleam@io:debug(Value),
erlang:error(#{gleam_error => panic,
message => <<"`panic` expression evaluated."/utf8>>,
module => <<"xmljson/internal/json"/utf8>>,
function => <<"parse"/utf8>>,
line => 16})
end.