Skip to content

Deserialization requires knowledge of fixed namespace; prevents robust/future-proofing coding #532

@transentia

Description

@transentia

I THINK this is a Request for Enhancement, rather than a true BUG report...

I am doing deserialization currently via:

@JsonIgnoreProperties(ignoreUnknown = true)
@JacksonXmlRootElement(localName = "cus:CustomerData")
public record CustomerData(
        @JsonProperty("cus:HouseNumber") String houseNumber,
        @JsonProperty("cus:StreetName") String streetName,
        ... etc.

and within a StdDeserializer, I use stuff like:

jsonNode.get("cus:PhoneNumber")

both of these assume that:

  • I know the namespace prefix string is going to be 'cus'
  • it won't change from my guess

Neither of these is true: the 'downstream' dev team may change things up at any time, so 'cus:XXX' may suddenly be sent to me as 'customerStuff:XXX' or the toolset vendor may change things around so that it becomes 'ns990875:XXX'. For whatever reason, this is all beyond my control.

It's hard to write robust deserializing code...

SO: I want to ignore the prefix.

I have a work-around for my StdDeserializer:

    private static JsonNode fieldEndingWith(JsonNode v, String suffix) {
        Iterator<Map.Entry<String, JsonNode>> fields = v.fields();
        while (fields.hasNext()) {
            Map.Entry<String, JsonNode> next = fields.next();
            if (next.getKey().endsWith(suffix))
                return next.getValue();
        }

        // should never get here...assuming a perfect world;-) but need to do something...
        // this isn't STRICTLY correct, but it works out and keeps all the rest of the code simple
        return v;
    }

I can replace jsonNode.get("cus:PhoneNumber") with fieldEndingWith(jsonNode, "PhoneNumber")

I can't do anything to 'tune' JsonProperty, etc.

So my RFE is: add an "ignorePrefix=true|false" option to JsonProperty and possibly JacksonXmlRootElement/JsonRootName.

I can see that this might cause problems with code like:

<x>
  <a:y />
  <b:y />
</x>

but one would presumably not be wanting to ignore namespacing in this situation (although evolving 'stuff' remains problematic).

Metadata

Metadata

Assignees

No one assigned

    Labels

    will-not-fixIssue for which there is no plan to fix as described or requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions