3

I have a structure which is an object at the top level, containing mainly strings as values and one nested object. It should look something like:

{
  "name" : "expand",
  "type" : "2",
  "code" : "...",
  "options" : {
     "atb" : {
         "description" : "..",
         "value" : true
     }
}

I'm guessing that because JSObject holds a list of key/value pairs, there is no way of mixing different value types on the same level. This seems like a huge limitation so I'm hoping I'm wrong!

2
  • Can the value of each key-value pair be an array or object? Commented Jun 4, 2013 at 15:35
  • I couldn't imagine it not being possible. My suspicion is that you just need to add a kv pair which has atb as the key and another JSObject as the value. Commented Jun 4, 2013 at 15:40

2 Answers 2

7

Text.JSON allows you to nest objects, as you can see by the type definition:

data JSValue
    = JSNull
    | JSBool     !Bool
    | JSRational !Rational
    | JSString   JSString
    | JSArray    [JSValue]
    | JSObject   (JSObject JSValue)

newtype JSObject e = JSONObject { fromJSObject :: [(String, e)] }

The type is recursive - JSValues may be JSObjects which in turn may be dictionaries of JSValues.

Sign up to request clarification or add additional context in comments.

Comments

1

If you don't already do it with generic, here is a way to use instances of TEXT.JSON

import Text.JSON

data SO = SO {
            name :: String,
            mytype :: Int,
            code :: String,
            options :: [Option]
        } deriving (Show)

data Option =Option {
                atb :: KV
            }


data KV = KV {
                desc :: String,
                v:: Bool
                 }

instance JSON SO where
   showJSON ge = makeObj
          [ ("name", showJSON $ name ge),
            ("type", showJSON $ mytype ge),
            ("options", showJSON $ options ge)
          ]                        
   readJSON = error "readJSON not implemented for SO"


instance JSON Option where
   showJSON ge = makeObj
          [ ("atb", showJSON $ atb ge)
          ]                        
   readJSON = error "readJSON not implemented for Option"

instance JSON KV where
   showJSON ge = makeObj
          [ ("description", showJSON $ desc ge),
          [ ("value", showJSON $ v ge)
          ]                        
   readJSON = error "readJSON not implemented for kv"

--encode $ SO .........

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.