Sign Up
Log In
Log In
or
Sign Up
Places
All Projects
Status Monitor
Collapse sidebar
home:Ledest:erlang:19
jwalk
jwalk-1.1.2-git.patch
Overview
Repositories
Revisions
Requests
Users
Attributes
Meta
File jwalk-1.1.2-git.patch of Package jwalk
diff --git a/README.md b/README.md index 6e5a8c5..4da67e8 100644 --- a/README.md +++ b/README.md @@ -163,7 +163,8 @@ set_p creates intermediary nodes: 1> jwalk:set_p({"users", {select, {"name", "sebastian"}}, "location"}, #{}, <<"Germany">>). #{<<"users">> => [#{<<"location">> => <<"Germany">>,<<"name">> => <<"sebastian">>}]} - 2> jwalk:set_p({"users", {select, {"name", "sebastian"}}, "location"}, [{}], <<"Germany">>). [{<<"users">>, + 2> jwalk:set_p({"users", {select, {"name", "sebastian"}}, "location"}, [{}], <<"Germany">>). + [{<<"users">>, [[{<<"name">>,<<"sebastian">>}, {<<"location">>,<<"Germany">>}]]}] diff --git a/doc/overview.edoc b/doc/overview.edoc index 8734dd2..b76dae4 100644 --- a/doc/overview.edoc +++ b/doc/overview.edoc @@ -4,6 +4,188 @@ jwalk @author Jim Rosenblum <jrosenblum@prodigy.net> @copyright 2015-2016 Jim Rosenblum -@version 1.1.1 +@version 1.1.2 @title Helper module for working with Erlang representations of JSON: EEP 18, map, Mochijson-style and Proplists +@doc + +This work is inspired by [ej](https://github.com/seth/ej), but handles all common JSON representations: eep-18, map, mochijson-style and proplists - the types returned by [jsone](https://github.com/sile/jsone), [jiffy](https://github.com/davisp/jiffy) and [mochijson](https://github.com/mochi/mochiweb), for example. +Anything good about this is probably due to the contributors and maintainers of +ej, anything bad or awkward is completely my fault. + +==Dependencies== +The source code is a single file, and there are no dependencies other than: +<ul> +<li> Erlang 17.0 +</li> +<li> Rebar3, for building</li> +</ul> +Hex package manager is used for package management. + + +===QuickStart=== +====clone==== + +$ git clone git://github.com/jr0senblum/jwalk.git + +$ cd jwalk + +====compile==== +$ make compile + +====run tests==== +$ make eunit + +==== dialyze==== +$ make dialyze + +==== Erlang shell==== +$ make start +``` + 1> jwalk:get({"one"},#{<<"one">> => 1}). + 1 + 2> jwalk:get({"one"},[{<<"one">>, 1}]). + 1 + 3> jwalk:get({"one"},{[{<<"one">>, 1}]}). + 1 + 4> jwalk:get({"one"},{struct, [{<<"one">>, 1}]}). + 1 +''' + + +==Functions== +Jwalk functions always take at least two parameters: a first parameter which is a +tuple of elements representing a Path into a JSON Object, and a second +parameter which is expected to be a valid JSON representation (map, proplist, etc.). + +<ul> +<li>jwalk:delete(Path, Object) -> Result + Removes the value at the location specified by Path from Object and returns a new structure +</li> +<li>jwalk:get(Path, Object) -> Result | undefined, + jwalk:get(Path, Object, Default) -> Result | Default + Returns the Value at the specificed Path from Object, or undefined or Default if not found </li> +<li>jwalk:set(Path, Object, Value) -> Result +Sets a Value in Object at the specified Path and returns the new structure </li> +<li>jwalk:set_p(Path, Object, Value) -> Result +Sets a Value in an Object at the specified Path creating intermediate nodes as necessary and +returns the new structure</li> +</ul> + +===Paths=== +Paths into JSON Objects are expressed using a tuple of Path elements, a +representation of a javascript-like path: i.e., +Obj.weapons.edged.distance becomes {"weapons","edged","distance"}. +Path elements representing JSON Member Names can be strings or binary, they +will be internally converted to binary regardless. + +In addition to the string/binary representations of Member Names, a Path element can +be +<ul> +<li> An integer index, or the atoms _first_ and _last_, which will select +an element out of a JSON Array.</li> +<li> {select, {"name","value"|value}} which will select a subset of JSON objects +from an Array that have a Member {"Name": "Value"|Value}</li> +<li> The atom _new_, used in conjunction with the functions set/2 and set_p/2, as +the final element of a Path will add the supplied value to the stucture as the first +element of an Array, the Array is created if necessary</li> +</ul> +Path, string elements can be binary or not, they will be converetd to binary regardless. + +Examples follow. + +===Usage Examples=== +``` + Weapons = [{<<"edged">>, [[{<<"type">>, <<"swords">>}, {<<"distance">>, <<"medium">>}], + [{<<"type">>, <<"bayonets">>}, {<<"distance">>, <<"medium">>}], + [{<<"type">>, <<"daggers">>}, {<<"distance">>, <<"close">>}] + ] + }]. + +then + + 1> jwalk:get({"edged", {select, {"distance","medium"}}},Weapons). + [[{<<"type">>,<<"swords">>},{<<"distance">>,<<"medium">>}], + [{<<"type">>,<<"bayonets">>},{<<"distance">>,<<"medium">>}]] + + 2> jwalk:get({"edged",{select,{"distance","medium"}},1},Weapons). + [{<<"type">>,<<"swords">>},{<<"distance">>,<<"medium">>}] + + 3> jwalk:get({"edged",{select,{"distance","medium"}},1,"type"},Weapons). + <<"swords">> + + % setting an element ... + 4> W2 = jwalk:set({"edged",3, "distance"}, Weapons, <<"very close">>). + [{<<"edged">>, + [[{<<"type">>,<<"swords">>},{<<"distance">>,<<"medium">>}], + [{<<"type">>,<<"bayonets">>},{<<"distance">>,<<"medium">>}], + [{<<"type">>,<<"daggers">>},{<<"distance">>,<<"very close">>}]]}] + + 5> jwalk:get({"edged","distance"},W2). + [<<"medium">>,<<"medium">>,<<"very close">>] + + + +Given a map: + + Obj = #{<<"widget">> => + #{<<"debug">> => <<"on">>, + <<"image">> => + #{<<"alignment">> => <<"center">>, + <<"hOffset">> => 250, + <<"name">> => <<"sun1">>, + <<"src">> => <<"Images/Sun.png">>, + <<"vOffset">> => 250}, + <<"keys">> => [], + <<"text">> => + #{<<"alignment">> => <<"center">>, + <<"data">> => <<"Click Here">>, + <<"hOffset">> => 250, + <<"name">> => <<"text1">>, + <<"onMouseUp">> => <<"sun1.opacity = (sun1.opacity / 100) * 90;">>, + <<"size">> => 36, + <<"style">> => <<"bold">>, + <<"vOffset">> => 100}, + <<"values">> => [1,2,3,4,5], + <<"version">> => <<"1">>, + <<"window">> => + #{<<"height">> => 500, + <<"name">> => <<"main_window">>, + <<"title">> => <<"Sample Konfabulator Widget">>, + <<"width">> => 500}}}. + +then + + 1> jwalk:get({"widget","debug"},Obj). + <<"on">> + + 2> jwalk:get({"widget","text"},Obj). + #{<<"alignment">> => <<"center">>, + <<"data">> => <<"Click Here">>, + <<"hOffset">> => 250, + <<"name">> => <<"text1">>, + <<"onMouseUp">> => <<"sun1.opacity = (sun1.opacity / 100) * 90;">>, + <<"size">> => 36, + <<"style">> => <<"bold">>, + <<"vOffset">> => 100} + + +set_p creates intermediary nodes: + + 1> jwalk:set_p({"users", {select, {"name", "sebastian"}}, "location"}, #{}, <<"Germany">>). + #{<<"users">> => [#{<<"location">> => <<"Germany">>,<<"name">> => <<"sebastian">>}]} + + 2> jwalk:set_p({"users", {select, {"name", "sebastian"}}, "location"}, [{}], <<"Germany">>). + [{<<"users">>, + [[{<<"name">>,<<"sebastian">>}, + {<<"location">>,<<"Germany">>}]]}] + + 3> jwalk:set_p({"users", {select, {"name", "sebastian"}}, "location"}, {[]}, <<"Germany">>). + {[{<<"users">>, + [{[{<<"name">>,<<"sebastian">>},r + {<<"location">>,<<"Germany">>}]}]}]} + + 4> jwalk:set_p({"users", {select, {"name", "sebastian"}}, "location"}, {struct,[]}, <<"Germany">>). + {struct,[{<<"users">>, + [{struct,[{<<"name">>,<<"sebastian">>}, + {<<"location">>,<<"Germany">>}]}]}]} +''' diff --git a/src/jwalk.erl b/src/jwalk.erl index 372e9c5..a91566f 100644 --- a/src/jwalk.erl +++ b/src/jwalk.erl @@ -150,7 +150,7 @@ %% {replacing_object_with_value, _} <br/> %% {index_out_of_bounds, _, _}. %% --spec delete(Path, Object) -> NewObject when +-spec delete(Path, Object) -> NewObject | no_return() when Path :: path(), Object :: j_obj(), NewObject :: j_obj(). @@ -178,7 +178,7 @@ delete(Path, Object) -> %% {replacing_object_with_value, _} <br/> %% {index_out_of_bounds, _, _}. %% --spec get(Path, Object) -> Result when +-spec get(Path, Object) -> Result | no_return() when Path :: path(), Object :: j_obj(), Result :: jwalk_return(). @@ -235,7 +235,7 @@ get(Path, Object, Default) -> %% {replacing_object_with_value, _} <br/> %% {index_out_of_bounds, _, _}. %% --spec set(Path, Object, Value) -> NewObject when +-spec set(Path, Object, Value) -> NewObject | no_return() when Path :: path(), Object :: j_obj(), Value :: value() | j_obj() | [value() | j_obj(),...],
Locations
Projects
Search
Status Monitor
Help
OpenBuildService.org
Documentation
API Documentation
Code of Conduct
Contact
Support
@OBShq
Terms
openSUSE Build Service is sponsored by
The Open Build Service is an
openSUSE project
.
Sign Up
Log In
Places
Places
All Projects
Status Monitor