Coder Social home page Coder Social logo

jsongrammar's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

jsongrammar's Issues

'toJson' on Example Person returns nothing

Reproduction

$ 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!

Support aeson 0.7

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'

Specialized parsers

The grammars contain information that could be used to generate specialized parsers that need fewer branches. Would this improve their performance?

Representing optional elements

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

type tags

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:)

Compilation fails on MacOSX Snow Leopard

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?

Representing Field Families

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

Add convenience function optionalProp

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.