-
Notifications
You must be signed in to change notification settings - Fork 354
Move depth to generate_json_data #903
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move depth to generate_json_data #903
Conversation
|
So this may be fine, but the reason I've put a TODO rather than a quick fix, is that until now If we move the |
|
Oh right there's definitely a missing case here: # This object is serialized to its depth in the JSON document.
class DepthToJSON
def to_json(state)
state = JSON::State.from_state(state)
state.depth.to_s
end
end
def test_state_depth
depth = DepthToJSON.new
assert_equal "[1]", JSON.generate([depth])
endThis passes on main but not on my branch. I think maybe through Can you share an example though? I'm not sure what you mean by calling |
I was thinking of something like this: Right now it fails with: Which is desirable. If we don't forward the depth, then I think it will end up in a much less nice |
Instead of incrementing JSON_Generator_State::depth, we now increment generate_json_data::depth, and only copied at the end.
For `JSON.generate` and `JSON::State#generate_new`, don't copy generate_json_data::depth to JSON_Generator_State::depth. In `JSON.generate`, the JSON_Generator_State is on the stack and discarded anyway. In `JSON::State#generate_new`, we copy the struct to avoid mutating the original one.
Now that the state isn't mutated in generate_new, we no longer need to copy the struct, we can just use it.
357d346 to
95f8bc7
Compare
|
Correct, the branch as it was did turn the |
I want to remove the memcpy of
generate_json_dataand remove the FIXME by putting the mutating depth where it should be, ingenerate_json_data. The State object itself still has a depth, for configuration.The idea is to have a new
generate_json_data::depthfield and stop mutating the oldJSON_Generator_State::depthone, and then at the end, copy the depth back to it. This doesn't need to happen in all cases, e.g.generate_newdoesn't need to copy it back, and this is what allows us not to do the memcpy it in the first place.This still keeps
generate_newto let it have different behaviors betweenJSON::Coder(callingdumpdoes not result in thedepthof theStatechanging if aJSON::NestingErroroccurs) andJSON::State(which does). I called that last behavior deprecated because I have another PR to start deprecating this behavior.