Comments (2)
The yyjson_mut_doc
is designed to create JSON data from scratch as quickly as possible. As a result, the memory pool is designed to be simple and not focused on releasing individual values. This allows for fast and easy creation and release of all values. You can find more information about it here: API.md#create-json-document
If you want to control the memory for individual values, a simpler solution would be to use calloc
or your own memory pool to create all yyjson_mut_val
, and then use yyjson_mut_set_xxx()
to assign values. Would this approach work for your needs?
from yyjson.
Usually a json object is obtained through Parse, In my opinion, remove the single yyjson_mut_val
from the original yyjson_mut_doc
, and then attach it as a child for another yyjson_mut_doc
is impossible under current circumstances.
jsonA: "{\"test1\": \"true\", \"test2\": false}"
jsonB: "{\"test1\": {\"test3\": \"nb\"}, \"test3\": false}"
we would like to make jsonA become {\"test1\": \"true\"}
and jsonB become "{\"test1\": {\"test3\": \"nb\"}, \"test2\": false,\"test3\": false}"
without any copy operation, something like the std::move
to saving time.
so, we define the
struct yyjson_mut_val {
uint64_t tag;
yyjson_val_uni uni;
yyjson_mut_val* next;
yyjson_val_chunk* val_chunk;
yyjson_str_chunk* str_chunk;
};
and change the
// do the same thing in unsafe_yyjson_str_pool_grow
bool unsafe_yyjson_val_pool_grow(yyjson_val_pool* pool, yyjson_alc* alc, usize count) {
yyjson_val_chunk* chunk;
usize size;
if (count >= USIZE_MAX / sizeof(yyjson_mut_val) - 16)
return false;
size = (count + 1) * sizeof(yyjson_mut_val);
// size = yyjson_max(pool->chunk_size, size);
chunk = (yyjson_val_chunk*)alc->malloc(alc->ctx, size);
if (yyjson_unlikely(!chunk))
return false;
chunk->next = pool->chunks;
pool->chunks = chunk;
pool->cur = (yyjson_mut_val*)(void*)((u8*)chunk + sizeof(yyjson_mut_val));
pool->end = (yyjson_mut_val*)(void*)((u8*)chunk + size);
// size = yyjson_min(pool->chunk_size * 2, pool->chunk_size_max);
// pool->chunk_size = size;
return true;
}
when we create new yyjson_mut_val, just record the chunk addr for both val/str. so we can tansfer the ownership when needed.
so we have defined several functions to solve the problem
yyjson_val_chunk* unsafe_yyjson_str_pool_shrink(yyjson_str_pool* pool, yyjson_mut_val* val);
yyjson_str_chunk* unsafe_yyjson_val_pool_shrink(yyjson_val_pool* pool,yyjson_mut_val* val);
bool unsafe_yyjson_str_pool_grow_without_malloc(yyjson_str_pool* pool, yyjson_str_chunk *chunk);
bool unsafe_yyjson_val_pool_grow_without_malloc(yyjson_val_pool* pool, yyjson_val_chunk* chunk);
// change the detach function
yyjson_mut_val* unsafe_yyjson_mut_val_detach(yyjson_mut_doc* old_doc, yyjson_mut_doc* doc, yyjson_mut_val* parent, yyjson_mut_val* item)
Maybe tonight, I would create the PR.
lol :) πtalk is cheap, I will show u the code.
from yyjson.
Related Issues (20)
- Support `yyjson_mut_obj_add_obj` & `yyjson_mut_obj_add_arr` like `yyjson_mut_arr_add_*` HOT 6
- mem leak HOT 1
- Static Code Analysis: Identical sub-expressions on both sides of operator "<<" HOT 1
- Shared libraries need to be properly versioned on Linux HOT 1
- Json files are not newline terminated when pretty printing HOT 3
- memory issue for recursive function HOT 7
- yyjson_ptr_get_sint() returns false for positive integer HOT 5
- How to change the object key in mutable object? HOT 2
- error: -Wno-implicit-int and -Wno-implicit-function-declaration detected HOT 2
- Support Schema validator HOT 1
- A character make fail the JSON (empty JSON) HOT 1
- Does yyjson have parameters to set decimal places? HOT 5
- Discussion / Feedback request: Incorporating bytearray type HOT 8
- incorrectly formatted bool fields. HOT 3
- I need to merge multiple JSON strings HOT 1
- Is there a method to store the array of primitive types? HOT 4
- Release life cycle HOT 3
- Remove static qualifiers to support wrapping in other languages HOT 5
- Report row and line number when generating errors, instead of an absolute position HOT 10
- Improve error messages HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from yyjson.