Skip to content

Commit

Permalink
Manual merge of #984 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Apr 8, 2023
1 parent bea951a commit 4f82b39
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 17 deletions.
64 changes: 64 additions & 0 deletions src/main/java/tools/jackson/core/JsonGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -1760,6 +1760,7 @@ public void copyCurrentEvent(JsonParser p) throws JacksonException
_copyCurrentIntValue(p);
break;
case ID_NUMBER_FLOAT:
// Different from "copyCurrentEventExact"!
_copyCurrentFloatValue(p);
break;
case ID_TRUE:
Expand All @@ -1779,6 +1780,69 @@ public void copyCurrentEvent(JsonParser p) throws JacksonException
}
}

/**
* Same as {@link #copyCurrentEvent} with the exception that copying of numeric
* values tries to avoid any conversion losses; in particular for floating-point
* numbers. This usually matters when transcoding from textual format like JSON
* to a binary format.
* See {@link #_copyCurrentFloatValueExact} for details.
*
* @param p Parser that points to the event to copy
*
* @throws WrappedIOException if there is an underlying I/O problem (reading or writing)
* @throws StreamReadException for problems with decoding of token stream
* @throws StreamWriteException for problems in encoding token stream
*/
public void copyCurrentEventExact(JsonParser p) throws JacksonException
{
JsonToken t = p.currentToken();
final int token = (t == null) ? ID_NOT_AVAILABLE : t.id();
switch (token) {
case ID_NOT_AVAILABLE:
_reportError("No current event to copy");
break; // never gets here
case ID_START_OBJECT:
writeStartObject();
break;
case ID_END_OBJECT:
writeEndObject();
break;
case ID_START_ARRAY:
writeStartArray();
break;
case ID_END_ARRAY:
writeEndArray();
break;
case ID_PROPERTY_NAME:
writeName(p.currentName());
break;
case ID_STRING:
_copyCurrentStringValue(p);
break;
case ID_NUMBER_INT:
_copyCurrentIntValue(p);
break;
case ID_NUMBER_FLOAT:
// Different from "copyCurrentEvent"!
_copyCurrentFloatValueExact(p);
break;
case ID_TRUE:
writeBoolean(true);
break;
case ID_FALSE:
writeBoolean(false);
break;
case ID_NULL:
writeNull();
break;
case ID_EMBEDDED_OBJECT:
writePOJO(p.getEmbeddedObject());
break;
default:
throw new IllegalStateException("Internal error: unknown current token, "+t);
}
}

/**
* Method for copying contents of the current event
* <b>and following events that it encloses</b>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,9 @@ public void testCopyCurrentEventBigDecimal() throws Exception {
try (JsonParser parser = JSON_F.createParser(ObjectReadContext.empty(), input)) {
parser.nextToken();
try (JsonGenerator generator = JSON_F.createGenerator(ObjectWriteContext.empty(), stringWriter)) {
generator.copyCurrentEvent(parser);
generator.copyCurrentEventExact(parser);
}
}
assertEquals(input, stringWriter.toString());
}

// [jackson-core#730]
/**
* Same as {@link #testCopyCurrentEventBigDecimal()} using copyCurrentStructure instead.
*/
public void testCopyCurrentStructureBigDecimal() throws Exception {
String input = "[1e999]";
StringWriter stringWriter = new StringWriter();
try (JsonParser parser = JSON_F.createParser(ObjectReadContext.empty(), input)) {
parser.nextToken();
try (JsonGenerator generator = JSON_F.createGenerator(ObjectWriteContext.empty(), stringWriter)) {
generator.copyCurrentStructure(parser);
}
}
assertEquals("[1E+999]", stringWriter.toString());
}
}

0 comments on commit 4f82b39

Please sign in to comment.