Giter Club home page Giter Club logo

Comments (2)

vincentguiling avatar vincentguiling commented on July 17, 2024

I write a converter from mpack_node_t to mpack_writer_t. It works well (except mpack_type_ext which I didn't use) and if currentNode->type is an array or a map, it will write all elements (key, value) by nested operations, which means finish function has been called automatically. It originate from (mpack_writer_t* writer, mpack_tag_t tag). And it only support change number to number.

example:

...//change node data externally
write_data_from_tree(writer, mpack_tree_root(tree));
void write_data_from_tree(mpack_writer_t* writer, mpack_node_t currentNode)
{
    mpack_node_data_t *value = currentNode.data;
    switch (value->type) {
        case mpack_type_missing:
            mpack_break("cannot write a missing value!");
            mpack_writer_flag_error(writer, mpack_error_bug);
            return;

        case mpack_type_nil:    mpack_write_nil   (writer);            return;
        case mpack_type_bool:   mpack_write_bool  (writer, value->value.b); return;
        case mpack_type_int:    mpack_write_int   (writer, value->value.i); return;
        case mpack_type_uint:   mpack_write_uint  (writer, value->value.u); return;

        case mpack_type_float:
            #if MPACK_FLOAT
            mpack_write_float
            #else
            mpack_write_raw_float
            #endif
                (writer, value->value.f);
            return;
        case mpack_type_double:
            #if MPACK_DOUBLE
            mpack_write_double
            #else
            mpack_write_raw_double
            #endif
                (writer, value->value.d);
            return;

        case mpack_type_str: mpack_write_str(writer, tree.data+value->value.offset, value->len); return;
        case mpack_type_bin: mpack_write_bin(writer, tree.data+value->value.offset, value->len); return;

        #if MPACK_EXTENSIONS
        case mpack_type_ext:
		#error data not include ext!
            mpack_start_ext(writer, mpack_tag_ext_exttype(&value), mpack_tag_ext_length(&value));
            return;
        #endif

        case mpack_type_array: 
			mpack_start_array(writer, value->len); 
			for(uint32_t i = 0; i < value->len; i++)
			{
				write_data_from_tree(writer, mpack_node(&tree, mpack_node_child(currentNode, i)));
			}
			mpack_finish_array(writer);
		return;
        case mpack_type_map:   
			mpack_start_map(writer, value->len);
			for(uint32_t i = 0; i < value->len; i++)
			{
				write_data_from_tree(writer, mpack_node(&tree, mpack_node_child(currentNode, i*2)));
                write_data_from_tree(writer, mpack_node(&tree, mpack_node_child(currentNode, i*2+1)));
            }
            mpack_finish_array(writer);
        return;
    }
    mpack_break("unrecognized type %i", (int)value.type);
    mpack_writer_flag_error(writer, mpack_error_bug);
}

from mpack.

ludocode avatar ludocode commented on July 17, 2024

Sorry, the node API parse tree is meant to be immutable. It's heavily optimized for this. I would strongly recommend against changing anything in the node API parse tree.

Making it mutable would drastically alter its efficiency. I've done extensive benchmarking of this in the past. Jansson is an example of a JSON parser that builds a mutable tree and it is the slowest parser on the list.

I don't currently have code to load MessagePack into a mutable tree that can be modified and written out again. If you wanted to implement something like this, it's not very hard. You could make your own mutable tree node struct and use the reader API to build it and the writer API to write it out again. It would only take a hundred or so lines of code.

from mpack.

Related Issues (20)

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.