-
Notifications
You must be signed in to change notification settings - Fork 748
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
JSON Serialization: Change Libraries #3225
JSON Serialization: Change Libraries #3225
Conversation
…hout main auction flow
…erator marshal call
expectedErrMessage: "invalid character", | ||
}, | ||
{ | ||
description: "Valid extension, empty extBidPrebid and invalid imp ext info", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did this test go away?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test was removed because the json-iterator Marshal
function does not validate json.RawMessage
fields like the standard library Marshal
function so an error in imp.ext
will go undetected. In that case I figured we should ensure that whatever is passed into this function is already known to be valid.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good. These were attempts to test the error handling of Marshal, which may very well be a fools errand.
@@ -403,22 +403,6 @@ func TestCreateSanitizedImpExt(t *testing.T) { | |||
}, | |||
expectedError: "", | |||
}, | |||
{ | |||
description: "Marshal Error - imp.ext.prebid", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did this test case go away?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same reason as above (#3225 (comment))
It looks good except I am unclear why 2 test cases went away. Also it seems we did not entirely do away with encoding/json |
Yeah I thought we just wanted to focus on updating the main auction flow for now but an update will be made to account for the other endpoints. The test utils should also be updated and perhaps we can reference |
^ These commits extend usage to all other parts of core code and tests. Added error message parsing to attempt to normalize the crazy format of json-iter. Don't want to send this stuff to publishers in a response. |
"GBP":0.7662523901 | ||
}, | ||
"GBP":0.4815162342 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The behavior of json and json-iter match here. The test was incorrectly setup. It failed because of the trailing comma, nothing to do with the repeated elements. Fixed the test and manually verified behavior matches.
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
// Execute: | ||
updatedRates := Rates{} | ||
err := json.Unmarshal([]byte(tc.ratesJSON), &updatedRates) | ||
err := jsonutil.UnmarshalValid([]byte(tc.ratesJSON), &updatedRates) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated all tests to use UnmarshalValid because it seemed like we want to validate in tests. Does that check out?
|
||
// Verify: | ||
assert.Equal(t, err != nil, tc.expectsError, tc.desc) | ||
if tc.expectsError { | ||
assert.Equal(t, err.Error(), tc.expectedError.Error(), tc.desc) | ||
assert.Equal(t, tc.expectedError.Error(), err.Error(), tc.desc) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Expected and actual orders were reversed, leading to confusing error messages. Fixed.
expectedErrMessage: "invalid character", | ||
}, | ||
{ | ||
description: "Valid extension, empty extBidPrebid and invalid imp ext info", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good. These were attempts to test the error handling of Marshal, which may very well be a fools errand.
|
||
msgStartIndex := strings.Index(msg, ": ") | ||
if msgStartIndex == -1 { | ||
return msg |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fallback to message as-is if markers aren't found.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good once the merge conflict is resolved
LGTM |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
This PR attempts to improve serialization performance by swapping out the standard library
encoding/json
with thejson-iterator
library.General notes about
json-iterator
:Notable differences between the standard library and json-iterator marshal/unmarshal functions:
json.RawMessage
field, if the field in the bytes has the value "null", the four byte value "null" is not written to thejson.RawMessage
field like it is in the standard library, but rather the value is considerednil
and as a result thejson.RawMessage
field will be emptyjson.RawMessage
, it is not validated. Syntax errors are not reported like they are with the standard library.Implementation:
UnmarshalValid
(which unmarshals and performs validation)Unmarshal
(which unmarshals without performing validation)Marshal
(which marshals without performing validation)Unmarshal
andMarshal
functionsjson-iterator
returns untyped string error messages with non-optional "context" for the parse error. We've tried our best to reverse their string formatting to extract a more reasonable error message. Expect this to be improved / fine tuned over time.