Git Product home page Git Product logo

nodemcu-json's Introduction

Express.js-like HTTP server library for NodeMCU

About

I wrote this JSON parser because the ESP8266 has very tight memory constraints.
A normal JSON parser requires you to load the JSON string into a variable and then craetes a gigantic table that contains all the information form the string. This is INCREDIBLY memory intensise.
But I came up with the ingenious idea of parsing a JSON string of almost arbitrary size by reading it from directly form a storage device (not RAM). It only stores one single character of the JSON string at a time in the RAM and immediately discards it if it is irrelevant. So now you can easily parse and access a JSON string that it 100 times bigger than your RAM wihtout any issues.

The interface

Using some metatable magic I created an interface that allows to access/parse a json string at a specified place, by almost accessing it like a table.
While you would access a table like this for instance: foo['bar'][123]['name'], you have to do foo['bar'][123]['name']() instead. If the value you are trying to access is an array/object then it will return a table that only contains the keys/indexes of the direct children.
This is enough to start iteration over it.

Use cases

I mainly developed this libary to be used by HTTP server libraries. The idea is that a json request body can be received in chunks and be written into a file.
This way you never have to store the json request body in your memory, but jsut on the flash.
Then this library can read the string from the file one byte at a time and parse it this way.

Examples

local example =  json('example.json') -- Specify which file you want to read the JSON string from

-- Read a value
local value = example["aa"][2]['k1']() -- Notice the brackets in the end!
print(value)

-- Loop over a key value table and print the keys and values
for key, value in pairs(example["aa"][2]()) do
    print('key: ' .. key, 'value: ' .. example["aa"][2][key]())
end

Need help? Have a feature request? Found a bug?

Create an issue right here on github.

nodemcu-json's People

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

hax-br

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.