Git Product home page Git Product logo

Comments (1)

nharrand avatar nharrand commented on September 27, 2024

As message pack is derivated from json, I think you'll need to tweak a little bit your array.
(On json Website you can find about json's grammar if that's a problem.)

But basically, your message need to be a map of objects "key":value, where key is a string, and value can be pretty much anything.
So if your message is just an array you can encode it as a map of 1 object.
Furthermore, you won't be able to have things like 1=>5, but you may have "1"=>5 if you want.

So if you want to send an array [1,2,3] you will have to send {"msgID":[1,2,3]}

// Map with 1 object
msgpck_write_map_header(&s, 1); //{
msgpck_write_string(&s, "msgID"); // "msgID"
//:
msgpck_write_array_header(&s, 3); //[
msgpck_write_integer(&s, 1); //1,
msgpck_write_integer(&s, 2); //2,
msgpck_write_integer(&s, 3); //3
//]
//}

If you want to have an array nested in another like{"msgID":[1,2,["mystring", nil, 123, 5]]}

// Map with 1 object
msgpck_write_map_header(&s, 1); //{
msgpck_write_string(&s, "msgID"); // "msgID"
//:
msgpck_write_array_header(&s, 3); //[
msgpck_write_integer(&s, 1); //1,
msgpck_write_integer(&s, 2); //2,

msgpck_write_array_header(&s, 4); //[
msgpck_write_string(&s, "mystring"); // "mystring",
msgpck_write_map_nil(); // nil,
msgpck_write_integer(&s, 123); //123,
msgpck_write_integer(&s, 5); //5
//]
//]
//}

If you have objects such as {"msgID":[1,2,{"pin1" : 20}} in your array you need to express them as map of 1 object

// Map with 1 object
msgpck_write_map_header(&s, 1); //{
msgpck_write_string(&s, "msgID"); // "msgID"
//:
msgpck_write_array_header(&s, 3); //[
msgpck_write_integer(&s, 1); //1,
msgpck_write_integer(&s, 2); //2,
msgpck_write_map_header(&s, 1); //{
msgpck_write_string(&s, "pin1"); // "pin1"
//:
msgpck_write_integer(&s, 20); //20
//}
//]
//}

Decoding on the arduino side is more tricky if you don't know in advance what your message will look like, but you can use the function msgpck_what_next(Stream * s); in order to read on the go the next type you'll be reading and switch acording to the result to the appropriate msgpck_read.

I hope it answers your question.

from arduino_msgpack.

Related Issues (5)

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.