Code Monkey home page Code Monkey logo

nimjson's Introduction

nimjson

nimjson generates nim object definitions from json documents. This was inspired by gojson.

Development

  • Nim (stable version)

Usage examples (CLI)

nimjson writes NilType type if a value or a first value of an array is null. Please fix NilType type yourself.

Large JSON

% curl -s https://api.github.com/repos/jiro4989/nimjson | nimjson -O:Repository
type
  NilType = ref object
  Repository = ref object
    id: int64
    node_id: string
    name: string
    full_name: string
    private: bool
    owner: Owner
    html_url: string
    description: string
    fork: bool
    url: string
    forks_url: string
    keys_url: string
    collaborators_url: string
    teams_url: string
    hooks_url: string
    issue_events_url: string
    events_url: string
    assignees_url: string
    branches_url: string
    tags_url: string
    blobs_url: string
    git_tags_url: string
    git_refs_url: string
    trees_url: string
    statuses_url: string
    languages_url: string
    stargazers_url: string
    contributors_url: string
    subscribers_url: string
    subscription_url: string
    commits_url: string
    git_commits_url: string
    comments_url: string
    issue_comment_url: string
    contents_url: string
    compare_url: string
    merges_url: string
    archive_url: string
    downloads_url: string
    issues_url: string
    pulls_url: string
    milestones_url: string
    notifications_url: string
    labels_url: string
    releases_url: string
    deployments_url: string
    created_at: string
    updated_at: string
    pushed_at: string
    git_url: string
    ssh_url: string
    clone_url: string
    svn_url: string
    homepage: string
    size: int64
    stargazers_count: int64
    watchers_count: int64
    language: string
    has_issues: bool
    has_projects: bool
    has_downloads: bool
    has_wiki: bool
    has_pages: bool
    forks_count: int64
    mirror_url: NilType
    archived: bool
    disabled: bool
    open_issues_count: int64
    license: License
    allow_forking: bool
    is_template: bool
    topics: seq[string]
    visibility: string
    forks: int64
    open_issues: int64
    watchers: int64
    default_branch: string
    temp_clone_token: NilType
    network_count: int64
    subscribers_count: int64
  Owner = ref object
    login: string
    id: int64
    node_id: string
    avatar_url: string
    gravatar_id: string
    url: string
    html_url: string
    followers_url: string
    following_url: string
    gists_url: string
    starred_url: string
    subscriptions_url: string
    organizations_url: string
    repos_url: string
    events_url: string
    received_events_url: string
    `type`: string
    site_admin: bool
  License = ref object
    key: string
    name: string
    spdx_id: string
    url: string
    node_id: string

Simple JSON

% nimjson examples/primitive.json
type
  NilType = ref object
  Object = ref object
    stringField: string
    intField: int64
    floatField: float64
    boolField: bool
    nullField: NilType

% nimjson examples/array.json
type
  NilType = ref object
  Object = ref object
    strArray: seq[string]
    intArray: seq[int64]
    floatArray: seq[float64]
    boolArray: seq[bool]
    nullArray: seq[NilType]
    emptyArray: seq[NilType]

% nimjson examples/object.json
type
  NilType = ref object
  Object = ref object
    point: Point
    length: int64
    responseCode: string
    debugFlag: bool
    rectangles: seq[Rectangles]
  Point = ref object
    x: float64
    y: float64
  Rectangles = ref object
    width: int64
    height: int64

JSON Schema

nimjson supports partially JSON Schema. nimjson generates Nim type definition with JSON Schema when you enable -j option.

$ nimjson -j examples/json_schema.json
type
  Object = ref object
    `type`: string
    id: string
    timestamp: string
    stream: string
    consumer: string
    consumer_seq: string
    stream_seq: string
    deliveries: int64
    domain: Option[string]

It is wrapped by default in the Option type that properties not included in the required parameter of JSON Schema. If you don't want to use Option type, you can use --disable-option-type option.

$ nimjson -j --disable-option-type examples/json_schema.json
type
  Object = ref object
    `type`: string
    id: string
    timestamp: string
    stream: string
    consumer: string
    consumer_seq: string
    stream_seq: string
    deliveries: int64
    domain: string

nimjson supports $ref and $defs keywords. But it doesn't support URL of $ref.

This is supported:

{
  "$id": "https://example.com/product.schema.json",
  "type": "object",
  "properties": {
    "product": { "$ref": "#/$defs/product" },
    "product2": { "$ref": "#/$defs/product2" }
  },
  "$defs": {
    "product": { "type": "string" },
    "product2": { "type": "array", "items": { "type": "string" } }
  }
}

This is NOT supported:

{
  "$id": "https://example.com/product.schema.json",
  "type": "object",
  "properties": {
    "product": { "$ref": "https://example.com/schemas/address" }
  }
}

Usage examples (API)

import nimjson

echo """{"keyStr":"str", "keyInt":1}""".toTypeString()

# Output:
# type
#   NilType = ref object
#   Object = ref object
#     keyStr: string
#     keyInt: int64

echo "examples/primitive.json".readFile().toTypeString("testObject")

# Output:
# type
#   NilType = ref object
#   TestObject = ref object
#     stringField: string
#     intField: int64
#     floatField: float64
#     boolField: bool
#     nullField: NilType

JSON Schema:

import nimjson

echo "examples/json_schema.json".readFile().toTypeString("testObject", jsonSchema = true)
type
  TestObject = ref object
    `type`: string
    id: string
    timestamp: string
    stream: string
    consumer: string
    consumer_seq: string
    stream_seq: string
    deliveries: int64
    domain: Option[string]

Installation

Nim users

nimble install nimjson

Linux users (Debian base distros)

wget https://github.com/jiro4989/nimjson/releases/download/v3.0.0/nimjson_3.0.0_amd64.deb
sudo dpkg -i ./nimjson*.deb

Linux users (RHEL compatible distros)

yum install https://github.com/jiro4989/nimjson/releases/download/v3.0.0/nimjson-3.0.0-1.el7.x86_64.rpm

Help

nimjson -h

nimjson generates nim object definitions from json documents.

Usage:
    nimjson [options] [files...]
    nimjson (-h | --help)
    nimjson (-v | --version)

Options:
    -h, --help                       Print this help
    -v, --version                    Print version
    -X, --debug                      Debug on
    -o, --out-file:FILE_PATH         Write file path
    -O, --object-name:OBJECT_NAME    Set object type name
    -p, --public-field               Public fields
    -q, --quote-field                Quotes all fields
    -j, --json-schema                Read JSON as JSON Schema format
        --disable-option-type        (Only JSON Schema) Disable using Option type

License

MIT

Document

Web application of nimjson

I created simple nimjson on web application.

https://jiro4989.github.io/nimjson

Javascript library of nimjson of the application is generated by this module (nimble js).

nimjson's People

Contributors

dependabot[bot] avatar jiro4989 avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar  avatar

nimjson's Issues

Duplicate types generated

I have a large json file where there are several duplicated keys, for different objects. This generates Nim types that clash as they have the same name.

Here's a minimal reproduction:

{
  "obj1": {
    "subtype": {"a": 1}
  },
  "obj2": {
    "subtype": {"b": 1}
  }
}

Running nimjson test.json gives this output:

type
  NilType = ref object
  Object = ref object
    obj1: Obj1
    obj2: Obj2
  Obj1 = ref object
    subtype: Subtype
  Subtype = ref object
    a: int64
  Obj2 = ref object
    subtype: Subtype
  Subtype = ref object
    b: int64

It is not valid nim due to the repeated Subtype declarations. I can obviously fix this manually but it would be nice if it generated them as Subtype1, Subtype2, etc.

Error: cannot open file: nimjson

I installed the library as suggested here: nimble install nimjson

I tried to use the API example but it does not work!

import nimjson
import json

echo """{"keyStr":"str", "keyInt":1}""".parseJson().toTypeString()
echo "examples/primitive.json".parseFile().toTypeString("testObject")

$ nim c -r test.nim
Hint: used config file '/home/hdias/.choosenim/toolchains/nim-0.20.0/config/nim.cfg' [Conf]
Hint: system [Processing]
Hint: widestrs [Processing]
Hint: io [Processing]
Hint: test [Processing]
/home/hdias/tmp/test.nim(1, 8) Error: cannot open file: nimjson

GitHub Pages整理

  • docsにHTMLファイルが残っている
  • 使っていないはず
  • 削除する

Check Keynames

Json Keynames can contain spaces and special characters and the Nim fields should be escaped with backquotes.
Example: https://pomber.github.io/covid19/timeseries.json

Also in this example there are a lot of repeating entries for different keys. Currently nimjson generates a object for every key which could maybe be changed.

public option

今は全部非公開フィールドなので、オプションで変更できるようにする

[Question] YAML status

Hello! This not really an issue, but just a question.

I've just found out that YAML 1.2 is a superset of the JSON schema, and the target is to maintain a baseline compatibility with it from v1.2.

Are you aware if it is possible to convert a YAML schema which is JSON compatible into a JSON schema to get Nim types generated automatically with nimjson?

Long story short, is there a route to go from YAML to Nim types passing through nimjson, in a way that I can then load JSON files that can be validated against that YAML schema in Nim into types generated by nimjson at compile time?

thanks!

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.