medeamelana / jsongrammar Goto Github PK
View Code? Open in Web Editor NEWCombinators for bidirectional JSON parsing
Home Page: http://hackage.haskell.org/package/JsonGrammar
License: Other
Combinators for bidirectional JSON parsing
Home Page: http://hackage.haskell.org/package/JsonGrammar
License: Other
$ git clone git://github.com/MedeaMelana/JsonGrammar.git
$ cd JsonGrammar
$ ghci -XTemplateHaskell
GHCi, version 7.0.4: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
Prelude> :l Examples.hs
*Example> toJson $ Person "Tom" Male 10 (Coords 0.0 0.0)
Nothing
Running GHCI version 7.0.4.
This makes it appear as if the library is broken in some way; the entire point of JsonGrammar is that toJson and fromJson works with only one JSON grammar mapping!
First, the version constraint has to be lifted.
Second, there's an actual compile error:
Language/JsonGrammar.hs:185:47:
No instance for (FromJSON ByteString)
arising from a use of `liftAeson'
Possible fix: add an instance declaration for (FromJSON ByteString)
In the expression: liftAeson
In an equation for `grammar': grammar = liftAeson
In the instance declaration for `Json ByteString'
Language/JsonGrammar.hs:186:47:
No instance for (FromJSON Lazy.ByteString)
arising from a use of `liftAeson'
Possible fix:
add an instance declaration for (FromJSON Lazy.ByteString)
In the expression: liftAeson
In an equation for `grammar': grammar = liftAeson
In the instance declaration for `Json Lazy.ByteString'
The grammars contain information that could be used to generate specialized parsers that need fewer branches. Would this improve their performance?
I am trying to capture the Json for the Telehash protocol (http://telehash.org/proto.html).
I am stuck on representing an optional field in the message.
So in the code snippet below,
data TeleHashEntry = TeleHashEntry
{ teleRing :: Int
, teleSee :: Maybe [T.Text]
, teleBr :: Int
, teleTo :: T.Text
-- , teleLine :: Maybe T.Text
-- , teleHop :: Maybe T.Text
} deriving (Eq, Show)
telexJson = $(deriveIsos ''TeleHashEntry)
instance Json TeleHashEntry where
grammar = telexJson . object
( prop "_ring"
. prop ".see"
. prop "_br"
. prop "_to"
-- . prop "_line"
-- . prop "_hop"
)
How do I capture that _line
and _hop
are optional?
The equivalent representation in Aeson is
instance FromJSON TeleHashEntry
where
parseJSON (Object v) = TeleHashEntry <$>
v .: "_ring" <*>
v .:? ".see" <*>
v .: "_br" <*>
v .: "_to" <*>
v .:? "_line" <*>
v .:? "_hop"
parseJSON _ = mzero
where the optional items are captured by .:?
Regards
Alan
PS, the balance of the code is at https://github.com/alanz/htelehash
I'm having a bit of trouble with a pattern.
If I have
data Foo = Bar Int | Baz Int
and I'd like
{ "type": "Bar", "id": 2 }
to be parsed to Bar 2
and
{"type": "Baz", "id": 2}
to be parsed to Baz 2, how would I arrange it?
Otherwise, a lovely piece of software, thank you:)
I get the following error when I try to install JsonGrammar under MacOSX Snow Leopard with GHC 7.0.3
sda-dsas-MacBook-Pro:src tom$ cabal install JsonGrammar
Resolving dependencies...
Configuring JsonGrammar-0.3.1...
Preprocessing library JsonGrammar-0.3.1...
Building JsonGrammar-0.3.1...
[1 of 5] Compiling Data.Iso.Core ( Data/Iso/Core.hs, dist/build/Data/Iso/Core.o )
[2 of 5] Compiling Data.Iso.TH ( Data/Iso/TH.hs, dist/build/Data/Iso/TH.o )
[3 of 5] Compiling Data.Iso.Common ( Data/Iso/Common.hs, dist/build/Data/Iso/Common.o )
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package array-0.3.0.2 ... linking ... done.
Loading package containers-0.4.0.0 ... linking ... done.
Loading package pretty-1.0.1.2 ... linking ... done.
Loading package template-haskell ... linking ... done.
Loading package semigroups-0.5.0.2 ... linking ... done.
Loading package bytestring-0.9.1.10 ... linking ... done.
Loading package deepseq-1.1.0.2 ... linking ... done.
Loading package attoparsec-0.9.1.2 ... linking ... done.
Loading package text-0.11.1.5 ... linking ... done.
Loading package blaze-builder-0.3.0.1 ... linking ... done.
Loading package double-conversion-0.2.0.1 ... : can't load .so/.DLL for: stdc++ (dlopen(libstdc++.dylib, 9): image not found)
cabal: Error: some packages failed to install:
JsonGrammar-0.3.1 failed during the building phase. The exception was:
ExitFailure 1
It seems to have something to do with this bug, http://hackage.haskell.org/trac/ghc/ticket/5289, but is there a workaround?
Another question. According to the Telehash protocol spec (http://telehash.org/proto.html), there can be multiple field in a given json message which are distinguished by a leading '+' character, followed by alpha-numerics. e.g. fields with names like "+end", or "+foo".
Without enumerating all possibilities, is there any way to gather these into a list or other structure in the underlying type?
e.g.
data TeleHashEntry = TeleHashEntry
{ teleRing :: Int
, teleSee :: Maybe [T.Text]
, teleBr :: Int
, teleTo :: T.Text
, teleLine :: Maybe T.Text
, teleHop :: Maybe T.Text
, teleSignals::[ (T.Text,T.Text)]
} deriving (Eq, Show)
telexJson = $(deriveIsos ''TeleHashEntry)
instance Json TeleHashEntry where
grammar = telexJson . object
( prop "_ring"
. prop ".see"
. prop "_br"
. prop "_to"
. optionalProp "_line"
. optionalProp "_hop"
. prop "+????" -- What should go here?
)
optionalProp :: Json a => String -> Iso (Object :- t) (Object :- Maybe a :- t)
optionalProp name = duck just . prop name <> duck nothing
Thanks
Alan
For optionally matching properties in objects:
optionalProp :: Json a => String -> Iso (Object :- t) (Object :- Maybe a :- t)
optionalProp name = duck just . prop name <> duck nothing
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.