• Stars
    star
    270
  • Rank 152,189 (Top 3 %)
  • Language
    Nim
  • License
    MIT License
  • Created almost 4 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A loose, direct to object json parser with hooks.

JSONy - A loose, direct to object json parser and serializer with hooks.

nimble install jsony

Github Actions

API reference

This library has no dependencies other than the Nim standard library.

About

Real world json is never what you want. It might have extra fields that you don't care about. It might have missing fields requiring default values. It might change or grow new fields at any moment. Json might use camelCase or snake_case. It might use inconsistent naming.

With this library you can use json your way, from the mess you get to the objects you want.

@[1, 2, 3].toJson() -> "[1,2,3]"
"[1,2,3]".fromJson(seq[int]) -> @[1, 2, 3]

Fast.

Currently, the Nim standard module first parses or serializes json into JsonNodes and then turns the JsonNodes into your objects with the to() macro. This is slower and creates unnecessary work for the garbage collector. This library skips the JsonNodes and creates the objects you want directly.

Another speed up comes from not using StringStream. Stream has a function dispatch overhead because it has to be able to switch between StringStream or FileStream at runtime. Jsony skips the overhead and just directly reads or writes to memory buffers.

Another speed up comes from parsing and readings its own numbers directly from memory buffer. This allows it to bypass string allocations that parseInt or $ create.

Serialize speed

name ............................... min time      avg time    std dv  times
treeform/jsony ..................... 1.317 ms      1.365 ms    ±0.054   x100
status-im/nim-json-serialization ... 2.043 ms      3.448 ms    ±0.746   x100
planetis-m/eminim .................. 5.951 ms      9.305 ms    ±3.210   x100
disruptek/jason .................... 6.858 ms      7.043 ms    ±0.125   x100
nim std/json ....................... 8.222 ms      8.510 ms    ±0.123   x100

Deserialize speed

name ............................... min time      avg time    std dv  times
treeform/jsony ..................... 4.134 ms      4.196 ms    ±0.052   x100
status-im/nim-json-serialization ... 7.119 ms     14.276 ms    ±2.033   x100
planetis-m/eminim .................. 7.761 ms      8.001 ms    ±0.277   x100
nim std/json ...................... 14.326 ms     14.473 ms    ±0.113   x100

Note: If you find a faster nim json parser or serializer let me know!

Can parse or serialize most types:

  • numbers and strings
  • seq and arrays
  • objects and ref objects
  • options
  • enums
  • tuples
  • characters
  • HashTables and OrderedTables
  • HashSets and OrderedSets
  • json nodes
  • and parseHook() enables you to parse any type!

Not strict.

Extra json fields are ignored and missing json fields keep their default values.

type Entry1 = object
  color: string
var s = """{"extra":"foo"}"""
var v = s.fromJson(Entry1)
doAssert v.color == ""

Converts snake_case to camelCase.

Nim usually uses camelCase for its variables, while a bunch of json in the wild uses snake_case. This library will convert snake_case to camelCase for you when reading json.

type Entry4 = object
  colorBlend: string

var v = """{"colorBlend":"red"}""".fromJson(Entry4)
doAssert v.colorBlend == "red"

v = """{"color_blend":"red"}""".fromJson(Entry4)
doAssert v.colorBlend == "red"

Has hooks.

Hooks are a powerful concept that allows you to parse json "your way" and is the main idea behind jsony!

  • Note: that hooks need to be exported to where you are parsing the json so that the parsing system can pick them up.

proc newHook*() Can be used to populate default values.

Sometimes the absence of a field means it should have a default value. Normally this would just be Nim's default value for the variable type but that isn't always what you want. With the newHook() you can initialize the object's defaults before the main parsing happens.

type
  Foo5 = object
    visible: string
    id: string
proc newHook*(foo: var Foo5) =
  # Populates the object before its fully deserialized.
  foo.visible = "yes"

var s = """{"id":"123"}"""
var v = s.fromJson(Foo5)
doAssert v.id == "123"
doAssert v.visible == "yes"

proc postHook*() Can be used to run code after the object is fully parsed.

Sometimes we need run some code after the object is created. For example to set other values based on values that were set but are not part of the json data. Maybe to sanitize the object or convert older versions to new versions. Here I need to retain the original size as I will be messing with the object's regular size:

type Sizer = object
  size: int
  originalSize: int

proc postHook*(v: var Sizer) =
  v.originalSize = v.size

var sizer = """{"size":10}""".fromJson(Sizer)
doAssert sizer.size == 10
doAssert sizer.originalSize == 10

proc enumHook*() Can be used to parse enums.

In the wild json enum names almost never match to Nim enum names which usually have a prefix. The enumHook*() allows you to rename the enums to your internal names.

type Color2 = enum
  c2Red
  c2Blue
  c2Green

proc enumHook*(v: string): Color2 =
  case v:
  of "RED": c2Red
  of "BLUE": c2Blue
  of "GREEN": c2Green
  else: c2Red

doAssert """ "RED" """.fromJson(Color2) == c2Red
doAssert """ "BLUE" """.fromJson(Color2) == c2Blue
doAssert """ "GREEN" """.fromJson(Color2) == c2Green

proc renameHook*() Can be used to rename fields at run time.

In the wild json field names can be reserved words such as type, class, or array. With the renameHook*() you can rename fields to what you want.

type Node = ref object
  kind: string

proc renameHook*(v: var Node, fieldName: var string) =
  if fieldName == "type":
    fieldName = "kind"

var node = """{"type":"root"}""".fromJson(Node)
doAssert node.kind == "root"

proc parseHook*() Can be used to do anything.

Json can't store dates, so they are usually stored as strings. You can use parseHook*() to override default parsing and parse DateTime as a string:

import jsony, times

proc parseHook*(s: string, i: var int, v: var DateTime) =
  var str: string
  parseHook(s, i, str)
  v = parse(str, "yyyy-MM-dd hh:mm:ss")

var dt = """ "2020-01-01 00:00:00" """.fromJson(DateTime)

Sometimes json gives you an object of entries with their id as keys, but you might want it as a sequence with ids inside the objects. You can handle this and many other scenarios with parseHook*():

type Entry = object
  id: string
  count: int
  filled: int

let data = """{
  "1": {"count":12, "filled": 11},
  "2": {"count":66, "filled": 0},
  "3": {"count":99, "filled": 99}
}"""

proc parseHook*(s: string, i: var int, v: var seq[Entry]) =
  var table: Table[string, Entry]
  parseHook(s, i, table)
  for k, entry in table.mpairs:
    entry.id = k
    v.add(entry)

let s = data.fromJson(seq[Entry])

Gives us:

@[
  (id: "1", count: 12, filled: 11),
  (id: "2", count: 66, filled: 0),
  (id: "3", count: 99, filled: 99)
]"""

proc dumpHook*() Can be used to serialize into custom representation.

Just like reading custom data types you can also write data types with dumpHook*(). The dumpHook() will receive the incomplete string representation of a given serialization (here s). You will need to add the serialization of your data type (here v) to that string.

type Fraction = object
  numerator: int
  denominator: int

proc dumpHook*(s: var string, v: Fraction) =
  ## Output fraction type as a string "x/y".
  s.add '"'
  s.add $v.numerator
  s.add '/'
  s.add $v.denominator
  s.add '"'

var f = Fraction(numerator: 10, denominator: 13)
let s = f.toJson()

Gives us:

"10/13"

proc skipHook*() Can be used to skip fields when serializing an object

If you want to skip some fields when serializing an object you can declare a skipHook*()

type
  Conn = object
    id: int
  Foo = object
    a: int
    password: string
    b: float
    conn: Conn

proc skipHook*(T: typedesc[Foo], key: static string): bool =
  key in ["password", "conn"]

var v = Foo(a:1, password: "12345", b:0.5, conn: Conn(id: 1))
let s = v.toJson()

Gives us:

"{"a":1,"b":0.5}"

Static writing with toStaticJson.

Sometimes you have some json, and you want to write it in a static way. There is a special function for that:

thing.toStaticJson()

Make sure thing is a static or a const value, and you will get a compile time string with your JSON.

Full support for case variant objects.

Case variant objects like this are fully supported:

type RefNode = ref object
  case kind: NodeNumKind  # The ``kind`` field is the discriminator.
  of nkInt: intVal: int
  of nkFloat: floatVal: float

The discriminator does not have to come first, if they do come in the middle this library will scan the object, find the discriminator field, then rewind and parse the object normally.

Full support for json-in-json.

Sometimes your json objects could contain arbitrary json structures, maybe event user defined, that could only be walked as json nodes. This library allows you to parse json-in-json were you parse some of the structure as real nim objects but leave some parts of it as Json Nodes to be walked later with code:

import jsony, json

type Entry = object
  name: string
  data: JsonNode

"""
{
  "name":"json-in-json",
  "data":{
    "random-data":"here",
    "number":123,
    "number2":123.456,
    "array":[1,2,3],
    "active":true,
    "null":null
  }
}""".fromJson(Entry)

Full support for raw-json.

Sometimes you don't need to parse the json, but just send it or store it in the database. You can speed this up by using RawJson type. What it does is prevents full parsing of that json tree and instead returns it is a RawJson (distinct string) type. You can then do anything you want with that. Store it in a database or pass it along to some other API. Or maybe parse it later again with jsony.

import jsony
type
  Message = object
    id: uint64
    data: RawJson

let
  messageData = """{"id":123,"data":{"page":"base64","arr":[1,2,3]}}"""
  message = messageData.fromJson(Message)

# make sure raw json was not parsed
doAssert message.data.string == """{"page":"base64","arr":[1,2,3]}"""

# make sure that dumping raw json produces same result
doAssert message.toJson() == messageData

You can also wait to parse the json later or maybe even with different types:

message.data.string.fromJson(DataPayload)

More Repositories

1

fidget

Figma based UI library for nim, with HTML and OpenGL backends.
Nim
765
star
2

pixie

Full-featured 2d graphics library for Nim.
Nim
741
star
3

ws

Simple WebSocket library for Nim.
Nim
253
star
4

genny

Generate a shared library and bindings for many languages.
Nim
224
star
5

puppy

Puppy fetches via HTTP and HTTPS
Nim
187
star
6

typography

Fonts, Typesetting and Rasterization.
Nim
151
star
7

shady

Nim to GPU shader language compiler and supporting utilities.
Nim
150
star
8

netty

Netty - reliable UDP connection library for games in Nim.
Nim
121
star
9

windy

Windowing library for Nim using OS native APIs.
Nim
117
star
10

nimtemplate

You can use this Nim template to jump start your Nim library or project.
Nim
110
star
11

chroma

Everything you want to do with colors, in Nim.
Nim
107
star
12

print

Print is a set of pretty print macros, useful for print-debugging.
Nim
105
star
13

boxy

2D GPU rendering with a tiling atlas.
Nim
103
star
14

vmath

Math vector library for graphical things.
Nim
98
star
15

pixie-python

Full-featured 2D graphics library for Python.
Python
96
star
16

flatty

Flatty - tools and serializer for plain flat binary files.
Nim
87
star
17

nim_emscripten_tutorial

Nim emscripten tutorial.
JavaScript
82
star
18

chrono

Chrono a Timestamps, Calendars, and Timezones library for nim.
Nim
78
star
19

spacy

Spatial data structures for Nim.
Nim
69
star
20

debby

Database ORM layer for Nim
Nim
67
star
21

flippy

Flippy is a simple 2d image and drawing library.
Nim
59
star
22

benchy

Benchmarking.
Nim
52
star
23

orbits

Orbits - orbital mechanics library for nim.
Nim
51
star
24

urlly

URL and URI parsing for Nim for C/JS backends. Similar api to browsers's window.location.
Nim
47
star
25

pretty

Pretty printer for Nim used for debugging.
Nim
47
star
26

steganography

Steganography - hide data inside an image.
Nim
46
star
27

hottie

Sampling profiler that finds hot paths in your code.
Nim
43
star
28

bumpy

2d collision library for Nim.
Nim
42
star
29

webby

Web utilities - HTTP headers, query parsing etc
Nim
42
star
30

thready

Alternative Interface for threads in Nim.
Nim
42
star
31

pg

Very simple PostgreSQL async api for nim.
Nim
38
star
32

slappy

3d sound api for nim.
C
37
star
33

glfm

Wrapper of GLFM (OpenGL ES and input for iOS and Android) library for Nim.
Nim
37
star
34

pixiebook

Nim
36
star
35

greenlet

Greenlet - Coroutines library for nim similar to python's greenlet.
C
34
star
36

googleapi

GoogleAPI access from nim.
Nim
33
star
37

word2vec

Word2vec implemented in nim.
Nim
32
star
38

llama2.nim

Inference Llama 2 in pure Nim
Nim
32
star
39

dumpincludes

See where your exe size comes from.
Nim
30
star
40

guardmons

Collection of nim shell utilities and libraries.
Nim
30
star
41

taggy

Everything to do with HTML and XML.
Nim
27
star
42

steamworks

Nim bindings to steamworks, valve's steam sdk.
Nim
26
star
43

asyncssh

SSH and run commands on other servers asynchronously for Nim.
Nim
25
star
44

sysinfo

Cross-platform way to find common system resources like, os, os version, machine name, cpu stats...
Nim
24
star
45

nimdocs

Automatic Nim document generator.
Nim
23
star
46

morepretty

Morepretty - like nimpretty but with more stuff.
Nim
22
star
47

icons2font

This utility takes vector icons in svg format and convert them to icon fonts (svg,ttf,waff,eot) to be display in all browsers.
Python
21
star
48

forematics

Formatics - Metamath verifier written in Nim.
Nim
20
star
49

openal

An OpenAL wrapper for nim.
Nim
18
star
50

globby

Glob pattern matching for Nim.
Nim
18
star
51

tabby

Direct to object CSV/TSV/tabulated data parser with hooks.
Nim
18
star
52

simple-fps

simple fps demo for panda3d
16
star
53

obj2egg

converts obj to egg format for panda3d
16
star
54

hobby

Treeform's hobby is to create Nim libraries that end with y.
14
star
55

nimby

Nimby is a very simple and unofficial package manager for nim language.
Nim
14
star
56

encode

Encode/decode utf8 utf16 and utf32.
Nim
14
star
57

miniz

Miniz wrapper for nim.
C
13
star
58

ptest

Print-testing for nim.
Nim
12
star
59

bitty

Tightly packed 1d and 2d bit arrays.
Nim
12
star
60

mddoc

Generated Nim's API docs in markdown for github's README.md files. Great for small libraries with simple APIs.
Nim
12
star
61

quickcairo

Nim Cairo bindings
Nim
11
star
62

digitalocean

Wrapper for DigitalOcean HTTP API.
Nim
10
star
63

useragents

User Agent parser for nim.
Nim
9
star
64

webkit2.net

My take on embedded webkit for .net (based on open-webkit-sharp, which is based on webkit.net)
JavaScript
8
star
65

statistics

Common and uncommon statistical functions for Nim.
Nim
8
star
66

jsutils

A library for nim that makes working with JS easier.
Nim
7
star
67

vt100terminal

Terminal VT100 emulation in pure Nim.
Nim
7
star
68

ssh

Connect to machines over SSH and run commands there from nim.
Nim
7
star
69

mpeg

Nim wrapper for pl_mpeg single header mpeg library.
C
7
star
70

onecup

Plain coffee script HTML templeting library.
CoffeeScript
6
star
71

supreme-tictactoe

Multi player tick tack toe web game using tornado, JQuery, and Jinja.
Python
5
star
72

proffy

Profiler for nim.
Nim
5
star
73

panda3d-CSM

panda3d's cascading shadowmaps sample thing
Python
5
star
74

meshDrawer

explains mesh Drawer element to panda3d folks
Python
5
star
75

istrolid-bugs

Official Istrolid Bug Tracking
4
star
76

freefrontfinder

A file that maps font names to where you can download them.
Nim
4
star
77

fidgetfonts

Collection of fonts that might be useful with fidget.
4
star
78

pystorm

pystrom is a python to javascript compiler based on Niall McCarroll's py2js released under MIT licence.
Python
4
star
79

asterisk

Web based code editor.
CoffeeScript
3
star
80

re.edit

Simple web editor based on Code Mirror
CoffeeScript
3
star
81

zlibstatic

Zlib Static - statically link zlib for nim.
C
3
star
82

consfyre

HTML5 2d spaceship building and flying game.
JavaScript
3
star
83

treescript

LISP like language that compiles to javascript.
JavaScript
3
star
84

panda3d-sample-models

I hope to come up with some nice models to use for panda3d
3
star
85

mysqler

a better command line client for mysql to replace the "the MySQL command-line tool"
Python
2
star
86

ddbase

Very dumb disk key-value store.
Python
2
star
87

Atomic4

May 10, 2004 - My first cool project.
Assembly
2
star
88

Re-edit

Client-server, in browser, code editor based on concept of shifting "boards"
Python
2
star
89

Java-Doc-Test

Simple implementation of Doc Tests for Java using Bean Shell
2
star
90

randcss

Generates a random CSS/HTML layout and style
2
star
91

basic-coffee-2d-html5-game-starter

for what you want to start a project with coffee quickly
2
star
92

eggOctree

eggOctree script for panda3d
2
star
93

treeterm

clone of ajaxterm and some changes
1
star
94

libuv

nimuv is libuv dynamic bind for nim.
C
1
star
95

nimbench

Set of nim benchmarks.
JavaScript
1
star
96

new-ajaxterm

My fork of ajaxterm.
Python
1
star
97

Java-Forms

Django-like "forms" implementation for java
1
star
98

strapless

easy coffeescript dynamic HTML MVC
JavaScript
1
star
99

panda3dmanual

converting panda3d manual from wiki-html mess into nice RST format
1
star
100

orekit

Fork of a free low-level space dynamics library: https://www.orekit.org/
Java
1
star