Giter Site home page Giter Site logo

hamidb80 / nimjson Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jiro4989/nimjson

0.0 0.0 0.0 132 KB

nimjson generates nim object definitions from json documents.

Home Page: https://jiro4989.github.io/nimjson/nimjson.html

License: MIT License

Nim 100.00%

nimjson's Introduction

nimjson

CI Status Install Status Doc Status

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

jiro4989 avatar dependabot[bot] 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.