Giter VIP home page Giter VIP logo

octave-rapidjson's Introduction

octave-rapidjson, a JSON parser for GNU Octave >= 3.8

GNU Octave wrapper around RapidJSON

These wrapper doesn't try to be compatible with jsondecode/jsonencode or JSONlab.

Instead it tries to map GNU Octave data structures as close as possible to JSON and back. So the major design goal is that in GNU Octave

assert (load_json (save_json (foo)), foo)

works for almost any type of "foo".

Function

load_json (json_string)

Importing data from a JSON string tries to preserve the datatype whenever possible and if this doesn't slown down the import.

octave> x = load_json ('{"a":5, "b":3.14156}')
x =

  scalar structure containing the fields:

    a = 5
    b =  3.1416

octave> typeinfo (x.a)
ans = uint32 scalar
octave> typeinfo (x.b)
ans = scalar

N-dimensional matrices are returned as matrix (aka "double matrix")

octave> x = load_json ('[3,4,5]')
x =

   3   4   5

octave> typeinfo (x)
ans = matrix

As soon as one element in a JSON array can't mapped to a double value, a cell is returned:

octave> x = load_json ('[2, "foo", 3.14]')
x =
{
  [1,1] =  2
  [1,2] = foo
  [1,3] =  3.1400
}

load from file

If you want to parse from a file use load_json (fileread (filename))

save_json (obj)

  • Saving a matrix always gives double values in JSON

Converting a struct array to JSON "unpacks" the struct array but I've been told by javascript programmers that this would be
the expected behaviour...

octave> foo(1).a = 3;
octave> foo(1).b = pi;
octave> foo(2).a = 5;
octave> foo(2).b = 2.718;
octave> save_json (foo)
ans = [
    {
        "a": 3.0,
        "b": 3.141592653589793
    },
    {
        "a": 5.0,
        "b": 2.718
    }
]

If you want to read it back (and collapse the unpacked struct) you can do:


octave> load_json (ans)
ans =
{
  [1,1] =

    scalar structure containing the fields:

      a =  3
      b =  3.1416

  [1,2] =

    scalar structure containing the fields:

      a =  5
      b =  2.7180

}

octave> x = [ans{:}]
x =

  1x2 struct array containing the fields:

    a
    b

Handling of NA, NaN, []

GNU Octave JSON
NA "null"
NaN "NaN"
[] "[]"
octave:1> save_json (NA)
ans = null
octave:2> save_json ([])
ans = []
octave:3> save_json (NaN)
ans = NaN
octave:4> load_json ("null")
ans = NA
octave:4> load_json ("[]")
ans = [](0x0)
octave:5> load_json ("NaN")
ans = NaN

Using "NaN" in JSON is an extension and may not be comaptible with other libraries.

Known problems / bugs / ToDo

Side note

If you want to start using the C++ API liboctave here are some links to useful resources:

Other JSON Parser for GNU Octave on github

octave-rapidjson's People

Contributors

andy1978 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.