fasterxml / jackson-datatypes-misc Goto Github PK
View Code? Open in Web Editor NEWCollection of common Jackson datatype modules not part of other multi-project repos
License: Apache License 2.0
Collection of common Jackson datatype modules not part of other multi-project repos
License: Apache License 2.0
(see PR #10 for background)
It seems useful to add overloads for module constructors of JSR-353/Jakarta-JSONP modules, to allow users to explicitly pass JsonProvider
instance to use, and not rely on auto-discovery via SPI. This is especially useful since it appears that "jakarta-jsonp" implementation has a classpath conflict with old jsr-353 implementation so that SPI fails if both new and old impl jars are in classpath (which should be fine; they are in different Java packages -- but isn't).
As of 2.12, no validation is done for case of missing monetary amount or unit, and failure propagates as NPE -- unit test even verifies that. But that seems like a pretty bad user experience. Instead, let's validate this properly and indicate using DeserializationContext.reportInputMismatch()
.
Probably also makes sense to check handling of currency unit as well; seems better to give Jackson-specific exception for unknown unit than expose library-specific one.
Hi!
Do you plan to support Jakarta JSON Processing?
The JavaEE packages are now being maintained and developed by the Jakarta project and Jakarta JSON v2.0.0 has been released recently. Unfortunately, v2.0.0 is not backwards compatible with 1.x.x because of package name change.ย
For Joda Money module, we have the possibility to select the amount representation on module level. Amount representation varies from API to API. In projects that involve integration with many APIs it would be beneficial to allow such selection on field-level. This ticket collects possible approaches.
To achieve field-level selection we could:
@JsonFormat
's shape
(see comment #1 and comment #2):@JsonFormat(shape = NUMBER_INT)
Money grandTotal;
Characteristics:
@JsonFormat
on Money (de)serializers as it won't be obvious what shape
s are supported and what effect they have.currencyCode
instead of currency
and value
instead of amount
as the names of the individual properties carrying the monetary amount. If we wanted to allow customization of the names in the future, most likely we wouldn't do that via @JsonFormat
annotation but rather a dedicated one.@JsonMoney
) allowing selection of amount representation (see comment):@JsonMoney(amountRepresentation = MINOR_CURRENCY_UNIT)
Money grandTotal;
Characteristics:
@JsonFormat
to allow passing (de)serializer-specific annotations (here: via new with
attribute):@JsonFormat(with = @JsonMoney(amountRepresentation = MINOR_CURRENCY_UNIT))
Money grandTotal;
Characteristics:
@JsonFormat
just for the sake of passing custom annotations.Regardless of the approach, most likely Contextual(De)Serializer
would be used to build the (de)serializer.
Do you see some more characteristics of these approaches?
Do you see a clear winner? (I'm slightly leaning towards approach 2)
Of course we don't have to pull the trigger now - might as well keep this open until we have more insights or convictions. Just wanted to write down my thoughts.
As the main task for JSTEP-1 (https://github.com/FasterXML/jackson-future-ideas/wiki/JSTEP-1) we need to:
as well as all secondary changes for OSGi and Java module system.
(note: re-try fix for FasterXML/jackson-datatype-jsr353#16)
So, as per earlier take, JSON null
should ideally map to JsonValue.NULL
.
However! If nominal target type is NOT JsonValue
but subtype like JsonObject
, this is not possible and value MUST remain Java null
.
I'm filing this issue as the result of conversation in pull request:
#32
Here is the test:
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsonorg.JsonOrgModule;
import org.json.JSONObject;
import org.junit.Assert;
import org.junit.Test;
public class JsonDoubleTest {
@Test
public void testDoubleParsing() throws Exception {
ObjectMapper objectMapper = new ObjectMapper().registerModule(new JsonOrgModule());
JSONObject jsonObject = objectMapper.readValue("{\"value\": 0.5 }", JSONObject.class);
Assert.assertEquals("java.lang.Double", jsonObject.get("value").getClass().getCanonicalName());
}
}
When I run it with Jackson 2.13.4 then it passes. When I run it with Jackson 2.15.0, it fails with error:
org.junit.ComparisonFailure:
Expected :java.lang.Double
Actual :java.math.BigDecimal
A JSON Merge Patch (RFC 7396) is typically a JSON object, and Jackson (at least versions 2.13 and 2.14.0-RC2) deserializes JSON objects to javax.json.JsonMergePatch
as expected.
However, when deserializing input that is not a JSON object, Jackson throws an exception, with the exact exception depending on whether the input is a JSON array or scalar.
RFC 7396 does not limit the JSON Merge Patch format to JSON Objects and in fact handles the case where the merge patch is not an object explicitly in the pseudocode function presented in Section 2 of the RFC. More importantly, Jackson's current behavior is somewhat awkward for instance when using JsonMergePatch
directly in a Spring controller like so:
@PatchMapping
ResponseEntity<?> patch(@RequestBody JsonMergePatch mergePatch) { ... }
When called with a body that is a JSON array, this throws an NPE, with a typical Spring exception handler would convert into a response with HTTP status code 500.
IMO, the preferred fix would be to call JsonValueDeserializer#deserialize
instead of _deserializeObject
from JsonMergePatchDeserializer
(see PR #26), which would return a JsonValue
. Alternatively, _deserializeObject
could check whether the current token is START_OBJECT
and return an empty JsonObject
.
The changes in 2.14 that have been merged from that branch into the master branch seem really good - for eg, we are looking for the serialization/deserialization changes that were done.
Unfortunately, since this hasnt been released, we ended up cobbling some code (not the best) to make our code work. But, would love if 2.14 can be released soon so that we can remove our wrapper code.
Looks like our org.json
dependency is bit old; there may be CVEs/vulns reported against intermediate versions and API should be compatible so let's just upgrade (for 2.15.0).
The test case com.fasterxml.jackson.datatype.jsonp.TestVersions
is failing, but because of its class name is currently being ignored by the build.
This is the part of FasterXML/jackson-databind#2434 that relates to changes to the jackson-datatype-jsr353 project.
To simplify the serialization of json-p patch objects, proposing adding delegating serializers for JsonPatch>JsonArray and JsonMergePatch>JsonValue
It takes extra steps to convert a patch into a type that has a serializer available.
addSerializer(JsonPatch.class, new StdDelegatingSerializer(new Converter<JsonPatch, JsonArray>() {
@Override
public JsonArray convert(JsonPatch value) {
return value.toJsonArray();
}
@Override
public JavaType getInputType(TypeFactory typeFactory) {
return typeFactory.constructFromCanonical(JsonPatch.class.getName());
}
@Override
public JavaType getOutputType(TypeFactory typeFactory) {
return typeFactory.constructFromCanonical(JsonArray.class.getName());
}
}));
(from: zalando/jackson-datatype-money#224)
Author existing jackson-datatype-money
seems supportive of the idea, to help release new versions, so it would be nice to integrate this, ideally in time for Jackson 2.12.0 (but if not immediate, soon after is fine too).
Describe the bug
Deserializing a missing property to JsonValue while using constructor properties should set java null instead JsonValue.null, this when deserializing to class with public properties works wel..
Version information
2.13.1
To Reproduce
JsonValueTest.txt
Expected behavior
When missing property map to java null and when property is null map to JsonValue.NULL.
Additional context
We are using this to know when we when we should do nothing (java null) and when we must set the value to null in database (JsonValue.NULL)
In the jsr353 module, the javax json api module require statement is using the wrong module name, resulting in this error:
java.lang.module.FindException: Module javax.json.api not found, required by com.fasterxml.jackson.datatype.jsr353
The "correct" name (in quotes because there is no consistency in the jakarta projects on how they named the javax modules :( ) is java.json.
So it should be
requires java.json;
rather than
requires javax.json.api;
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.