jsonparser icon indicating copy to clipboard operation
jsonparser copied to clipboard

ArrayEach + Set lost symbols

Open sergolius opened this issue 7 years ago • 3 comments

Example: you have array of maps, and you need add extra fields to each element of array. This may cause an issue, if maps are big enough. After extending each map result array ([]byte) may reach out of range and you will lost some number of symbols in result.

Code example: https://play.golang.org/p/3QcQtupAfIr

p.s. Update data by slice(referencing) is not obvious.

sergolius avatar Aug 14 '18 16:08 sergolius

@sergolius did you ever find a solution to this?

dcelasun avatar Sep 03 '19 13:09 dcelasun

@dcelasun I didn't dig into it much. I copying original value to v inside of .ArrayEach to break reference and operate with copy:

_, err := jsonparser.ArrayEach([]byte(data),
	func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
		v := make([]byte, len(value))
		copy(v, value) 
...
                v, _ = jsonparser.Set(v, []byte(`"new_value"`), "key")

sergolius avatar Sep 03 '19 14:09 sergolius

Yeah, I've ended up doing the same. Thanks anyway.

dcelasun avatar Sep 03 '19 14:09 dcelasun