-
Notifications
You must be signed in to change notification settings - Fork 264
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
Allow using precise floats in logs #2005
Open
kasmarian
wants to merge
6
commits into
main
Choose a base branch
from
#1993-allow-using-precise-floats
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
712af2a
Allow using precise floats in logs
kasmarian 27515ed
extract usePreciseFloats check into a separate method
kasmarian 65bcb76
add a comment about performance penalty
kasmarian 20ba266
add strategy to handle different ways of handling floats
kasmarian 2499c37
leve only one level of wrappers (remove creators)
kasmarian 4620501
update README
kasmarian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
logbook-json/src/main/java/org/zalando/logbook/json/DefaultJsonGeneratorWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.zalando.logbook.json; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
|
||
final class DefaultJsonGeneratorWrapper extends JsonGeneratorWrapper { | ||
public DefaultJsonGeneratorWrapper(JsonGenerator delegate) { | ||
super(delegate); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
logbook-json/src/main/java/org/zalando/logbook/json/DefaultJsonGeneratorWrapperCreator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package org.zalando.logbook.json; | ||
|
||
import com.fasterxml.jackson.core.JsonFactory; | ||
|
||
import java.io.CharArrayWriter; | ||
import java.io.IOException; | ||
|
||
public final class DefaultJsonGeneratorWrapperCreator implements JsonGeneratorWrapperCreator { | ||
public JsonGeneratorWrapper create(JsonFactory factory, CharArrayWriter output) throws IOException { | ||
return new DefaultJsonGeneratorWrapper(factory.createGenerator(output)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
logbook-json/src/main/java/org/zalando/logbook/json/JsonGeneratorWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package org.zalando.logbook.json; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.core.JsonParser; | ||
|
||
import java.io.Closeable; | ||
import java.io.Flushable; | ||
import java.io.IOException; | ||
|
||
public abstract class JsonGeneratorWrapper implements Closeable, Flushable { | ||
protected final JsonGenerator delegate; | ||
|
||
public JsonGeneratorWrapper(JsonGenerator delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
public void copyCurrentEvent(JsonParser parser) throws IOException { | ||
delegate.copyCurrentEvent(parser); | ||
} | ||
|
||
public void useDefaultPrettyPrinter() { | ||
delegate.useDefaultPrettyPrinter(); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
delegate.close(); | ||
} | ||
|
||
@Override | ||
public void flush() throws IOException { | ||
delegate.flush(); | ||
} | ||
|
||
public void writeString(String text) throws IOException { | ||
delegate.writeString(text); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
logbook-json/src/main/java/org/zalando/logbook/json/JsonGeneratorWrapperCreator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.zalando.logbook.json; | ||
|
||
import com.fasterxml.jackson.core.JsonFactory; | ||
|
||
import java.io.CharArrayWriter; | ||
import java.io.IOException; | ||
|
||
public interface JsonGeneratorWrapperCreator { | ||
JsonGeneratorWrapper create(JsonFactory factory, CharArrayWriter output) throws IOException; | ||
} |
11 changes: 11 additions & 0 deletions
11
logbook-json/src/main/java/org/zalando/logbook/json/NumberAsStringJsonGeneratorWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package org.zalando.logbook.json; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
|
||
final class NumberAsStringJsonGeneratorWrapper extends JsonGeneratorWrapper { | ||
@SuppressWarnings("deprecation") | ||
public NumberAsStringJsonGeneratorWrapper(JsonGenerator delegate) { | ||
super(delegate); | ||
delegate.enable(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...son/src/main/java/org/zalando/logbook/json/NumberAsStringJsonGeneratorWrapperCreator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.zalando.logbook.json; | ||
|
||
import com.fasterxml.jackson.core.JsonFactory; | ||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.core.json.JsonWriteFeature; | ||
|
||
import java.io.CharArrayWriter; | ||
import java.io.IOException; | ||
|
||
|
||
public final class NumberAsStringJsonGeneratorWrapperCreator implements JsonGeneratorWrapperCreator { | ||
public JsonGeneratorWrapper create(JsonFactory factory, CharArrayWriter output) throws IOException { | ||
final JsonGenerator generator = factory.rebuild() | ||
.enable(JsonWriteFeature.WRITE_NUMBERS_AS_STRINGS) | ||
.build() | ||
.createGenerator(output); | ||
return new NumberAsStringJsonGeneratorWrapper(generator); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
logbook-json/src/main/java/org/zalando/logbook/json/PreciseFloatJsonGeneratorWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.zalando.logbook.json; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.core.JsonParser; | ||
|
||
import java.io.IOException; | ||
|
||
final class PreciseFloatJsonGeneratorWrapper extends JsonGeneratorWrapper { | ||
|
||
public PreciseFloatJsonGeneratorWrapper(JsonGenerator delegate) { | ||
super(delegate); | ||
} | ||
|
||
@Override | ||
public void copyCurrentEvent(JsonParser parser) throws IOException { | ||
delegate.copyCurrentEventExact(parser); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...-json/src/main/java/org/zalando/logbook/json/PreciseFloatJsonGeneratorWrapperCreator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package org.zalando.logbook.json; | ||
|
||
import com.fasterxml.jackson.core.JsonFactory; | ||
|
||
import java.io.CharArrayWriter; | ||
import java.io.IOException; | ||
|
||
public final class PreciseFloatJsonGeneratorWrapperCreator implements JsonGeneratorWrapperCreator { | ||
public JsonGeneratorWrapper create(JsonFactory factory, CharArrayWriter output) throws IOException { | ||
return new PreciseFloatJsonGeneratorWrapper(factory.createGenerator(output)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@msdousti as per
WRITE_NUMBERS_AS_STRINGS
feature, it looks like it doesn't work the way we thought.JsonToken type is still ID_NUMBER_FLOAT and JsonGenerator calls _copyCurrentFloatValue, which calls
WriterBasedJsonGenerator.writeNumber(double d) and the double value is written down to the write, just wrapped in a string.
I left this failing test as well as
NumberAsStringJsonGeneratorWrapperCreator
implementation as a showcase.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.
@kasmarian
I should admit
enable(JsonWriteFeature.WRITE_NUMBERS_AS_STRINGS)
was a bad suggestion as it is used during writing of an object to JSON, not reading of a JSON string.Unfortunately, I could not find a similar JsonReadFeature.
Alternatively, I came up with the following code. The compact function can be adapted to look like this or something similar.
Output:
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.
Good suggestion with using bare
(parser.getValueAsString())
! It's straightforward, and the functionality is on the same level, that these filters are operating on, as opposed to factory feature flags, and I didn't like as much. I applied it inNumberAsStringJsonGeneratorWrapper
. Please have a look.If you think we should switch from the strategy approach to feature flags (enums), let's discuss it in the other thread, please.