Giter Club home page Giter Club logo

json's Introduction

JSON for Lua

License TrivisStatus AppVeyorStatus

A very fast json module for LuaJIT 2.0/2.1 and Lua 5.1/5.2/5.3.

Based on the very fast json library RapidJSON.

See project homepage for more informations, bug report and feature request.

Install

luarocks install json

Test

Clone or download source code, in the project root folder:

luarocks install dromozoa-utf8
luarocks install busted
luarocks make
busted

Performance

lua performance/run.lua

The CI will also run the performance test at the end of build. See build log travis and appveyor for details.

API

json.decode()

Decode json to lua table.

Synopsis

value = json.encode(jsonstring)

Arguments

jsonstring

A json value string to be decoded.

Returns

Return table if json is an object or array.

Return true, false, number and json.null respectively if json is a simple value.

Return nil plus an error message as a second result when passed string is not valid json string.

Errors

  • When passed value is not (convertable to) string.

json.encode()

Encode lua table to json string.

supports the following types:

  • boolean
  • function (json.null only)
  • number
  • string
  • table

The json object keys are sorted by the this function.

Synopsis

string = json.encode(value [, option])

Arguments

value:

When passed a table:

  1. Trade as array if:
    • metatable field __jsontype set to array.
    • table contains only integer keys from 1 to n.
  2. Otherwise the table are trade as object and integer keys are converted to string.

When passed with true, false, number and json.null, simply encode as simple json value.

option:

A optional table contains follow field:

  • pretty boolean: true to make output string to be pretty formated.

Returns

Return encoded json string on success. Return nil on failure, plus an error message as a second result.

Errors

  • When option passed a value other than table.

Examples

local json = require('json')

json.encode({})     -- '{}'

json.encode(json.object()) --> '{}'
json.encode(json.array()) --> '[]'

json.encode(setmetatable({}, {__jsontype='object'})) --> '{}'
json.encode(setmetatable({}, {__jsontype='array'})) --> '[]'

json.encode(true) --> 'true'
json.encode(json.null) --> 'null'
json.encode(123) --> '123.0' or '123' in Lua 5.3.


json.encode({true, false}) --> '[true, false]'

json.encode({a=true, b=false}) --> '{"a":true,"b":false]'

json.load()

Load json file into lua table.

Synopsis

value = json.load(filename)

Arguments

filename

Json file to be loaded.

Returns

Return table if file contains an object or array.

Return true, false, number and json.null respectively if file contains a simple value.

Return nil plus an error message as a second result when passed file is not valid json file.

Errors

  • When passed filename is not (convertable to) string.

json.dump()

Dump lua value to json file.

Synopsis

success, err = json.dump(value, filename [, option])

Arguments

value

Same as in json.encode().

filename

The file path string where to save dumpped json.

option:

A optional table contains follow field:

  • pretty boolean: true to make output string to be pretty formated.

Returns

bool: success

Return true on success.

Return false plus an error message as a second result when:

  • Value can't be encoded.
  • filename can't be opened for write.

Error

  • When passed filename is not (convertable to) string.
  • When passed option is not table, nil or none.

Example

local json = require('json')

json.dump({json.null}, 'test.json')
json.dump({json.null}, 'test-pretty.json', {pretty=true})

json.null

The placeholder for null values in json.

eg.

local json = require('json')

json.decode('[null]') --> {json.null}
json.encode({json.null}) --> '[null]'

json.object()

Create a new empty table that have metatable field __jsontype set as 'object' so that the encode and dump function will encode it as json object.

When passed an valid table:

  • Passed table do not have metatable, just set above metatable for the table.
  • Passed table already have metatable, just the the metatable field __jsontype to 'object'.

Synopsis

obj = json.object([t])

Arguments

t

Optinal table to be set the metatable with meta field __jsontype set as 'object'.

Returns

Origin passed in table when passed with a table. Or new created table.

json.array()

Same as json.array() except the metatable field __jsontype is set as 'array'. And the encode and dump function will encode it as json array.

Changelog

0.2.0[WIP]

  • json.object() and json.array() just set metatable field __jsontype to 'object' and 'array' it passed table already have a metatable.
  • fixes dump return value of false rather than nil.

0.1.0

  • Initial release.

json's People

Contributors

xpol avatar starius avatar

Stargazers

toshi_k avatar

Watchers

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