Permalink
Browse files

Merge pull request #2 from aliyakamercan/master

Fleshed out a bit and made compatible to lastest exprecs
  • Loading branch information...
2 parents c5712bd + 73c4cb1 commit 52d3529f4caa144701ebdd8d3e6252e8bf079e8a @justinkirby committed Dec 15, 2012
Showing with 1,411 additions and 635 deletions.
  1. +5 −0 .gitignore
  2. +0 −4 README
  3. +49 −0 README.md
  4. +0 −5 TODO.org
  5. +3 −1 rebar.config
  6. +0 −480 src/exprecs.erl
  7. +2 −122 src/json_rec.erl
  8. +1 −4 src/json_rec_model.erl
  9. +1 −19 src/json_rec_model.example
  10. +889 −0 src/mochijson2.erl
  11. +354 −0 src/mochinum.erl
  12. +107 −0 test/json_rec_tests.erl
View
@@ -1 +1,6 @@
+.eunit
+deps
+*.o
+*.beam
+*.plt
ebin
View
4 README
@@ -1,4 +0,0 @@
-I will eventually flesh this out. This is a quick yank out of a larger
-private repos. So forgive the crudeness.
-
-I have included exprecs.erl in the src for convenience.
View
@@ -0,0 +1,49 @@
+####Making erlang record <~> json conversion easy.
+
+Detailed info at [exprecs, making json usable](http://blogs.openaether.org/?p=253 "json_rec")
+
+Quick example:
+--
+ -module(book).
+
+ -export([new/1]).
+
+ -record(book, {
+ style :: atom(),
+ count :: integer(),
+ available :: boolean(),
+ pages :: integer(),
+ excerpt :: string(),
+ author :: string()
+ }).
+
+ %% the exprecs export of the record interface
+ -compile({parse_transform, exprecs}).
+ -export_records([book]).
+
+ %% here we provide a mapping of the json key to a record.
+ new(<<"book">>) ->
+ '#new-book'();
+
+ %% if the key is unknown, return undefined.
+ new(_RecName) ->
+ undefined.
+
+At this point we can take the following json and transform it into the #book{} record.
+
+ {
+ "style": "fiction",
+ "count": 1,
+ "available": true,
+ "pages": 42,
+ "excerpt": "Good bye and thanks for all the fish.",
+ "author":"Adams, Douglas"
+ }
+We can get a #book{} record from the above with
+
+
+ -spec json_to_rec(Json :: string()) -> #book{}.
+ json_to_rec(Json) ->
+ ErlJson = mochijson2:decode(Json),
+ Record = book:new(<<"book">>),
+ json_rec:to_rec(ErlJson, book, Record).
View
@@ -1,5 +0,0 @@
-* TODO move unit tests to file under tests dir
-* TODO flesh out a handy readme file based on upcoming blog post.
- The blog post has been written. this git repos is here so it can
- point to something
-
View
@@ -1,4 +1,6 @@
%%-*- mode: erlang; -*-
-{deps, [ ]}.
+{deps, [
+ {'parse_trans', "", {git, "git@github.com:uwiger/parse_trans.git", {branch, "master"}}}
+ ]}.
{erl_opts, [debug_info]}.
Oops, something went wrong.

0 comments on commit 52d3529

Please sign in to comment.