Packages

Convert between XML and JSON using the BadgerFish specification

Current section

Files

Jump to
xmljson src xmljson.erl
Raw

src/xmljson.erl

-module(xmljson).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([to_json/1, to_xml_fragment/1, to_xml/1]).
-file("/home/edwin/Documents/code/gleam/badgerfish/src/xmljson.gleam", 10).
-spec to_json(binary()) -> simplejson@jsonvalue:json_value().
to_json(Input) ->
Input@1 = begin
_pipe = Input,
_pipe@1 = xmlm:from_string(_pipe),
xmlm:with_stripping(_pipe@1, true)
end,
_assert_subject = xmlm:signal(Input@1),
{ok, {{dtd, _}, Input@2}} = case _assert_subject of
{ok, {{dtd, _}, _}} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"xmljson"/utf8>>,
function => <<"to_json"/utf8>>,
line => 16})
end,
{Lst, _} = xmljson@internal@xml:parse_xml(Input@2, [], [], maps:new()),
{json_object, xmljson@internal@xml:dict_from_list(Lst)}.
-file("/home/edwin/Documents/code/gleam/badgerfish/src/xmljson.gleam", 22).
-spec to_xml_fragment(simplejson@jsonvalue:json_value()) -> binary().
to_xml_fragment(Input) ->
_assert_subject = xmljson@internal@json:parse(Input),
[X] = case _assert_subject of
[_] -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"xmljson"/utf8>>,
function => <<"to_xml_fragment"/utf8>>,
line => 23})
end,
_pipe = xmb_ffi:identity(X),
unicode:characters_to_binary(_pipe).
-file("/home/edwin/Documents/code/gleam/badgerfish/src/xmljson.gleam", 28).
-spec to_xml(simplejson@jsonvalue:json_value()) -> binary().
to_xml(Input) ->
_pipe = xmljson@internal@json:parse(Input),
_pipe@1 = xmb:render(_pipe),
unicode:characters_to_binary(_pipe@1).