GithubHelp home page GithubHelp logo

jsonob's Introduction

jsonob: JSON / Object Mapper

marshal from the stdlib allows you to serialize your arbitrary nim objects into JSON strings and get your objects back from those serialized JSON strings. But what if, you want to exchange data in JSON strings between Web APIs and your code? marshal doesn’t work any more because:

  • It can’t parse JSON null correctly.

  • It can’t serialize Option[T] properly (it just serialize all the fields of it, but it should really be null for none(T) and value for some(value))

  • It is designed for serialize arbitrary nim objects, instead of requiring you to model your nim objects around JSON.

That’s where jsonob come in. You model your nim objects directly around the JSON data. jsonob serialize it to JSON, or parse the JSON to your objects.

Example

import json, jsonob, options

type
    Result = object
        status: Status
        messages: seq[Message]
        error: Option[string]       # exists only when status == failed

    Status = enum
        ok
        failed

    Message = object
        `from`: Option[string]    # none for "anonymous"
        text: string
        date: Date

    Date = tuple
        year: int
        month: int
        day: int


proc test(s: string) =
    let r = s.parse_json.to Result

    case r.status
    of Status.ok:
        for message in r.messages:
            echo message.`from`
            echo message.text
            echo message.date
    of Status.failed:
        echo "failed"
        echo r.error.get()

    echo()

test """
{
    "status": "ok",
    "messages": [
        {
            "text": "hello world",
            "date": [2016, 7, 15]
        }
    ]
}
"""

test """
{
    "status": "failed",
    "messages": [],
    "error": "that's an error"
}
"""
The output
None[string]
hello world
(year: 2016, month: 7, day: 15)

failed
that's an error

TODO

  • support for object variant

type
    Result = object
        case status: Status
        of Status.ok:
            messages: seq[Message]
        of Status.failed:
            error: string
  • better error message

  • documentation

jsonob's People

Watchers

James Cloos avatar

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.