question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

deserialize to static List<T> field get double repeat value

See original GitHub issue

Testcase:

public class Test {
    private static final ObjectMapper MAPPER = new ObjectMapper();

    static {
        MAPPER.registerModule(new JodaModule());
        MAPPER.registerModule(new JodaTimeModule());
        MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        MAPPER.configure(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT, true);
        MAPPER.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
        MAPPER.configure(com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    }

    protected static List<String> list = new ArrayList();

    protected static String str = "str";

    public List<String> getList() {
        return list;
    }

    public String getStr() {
        return str;
    }

    public static void main(String[] args) {
        Test test = new Test();
        test.getList().add("1");
        String jsonString = MAPPER.writeValueAsString(test);
        System.out.println(jsonString);
        Test test1 = MAPPER.readValue(jsonString, Test.class)
        System.out.println(test1.getList());
        System.out.println(test1.getStr());
    }
}

Result: {“str”:“str”,“list”:[“1”]} [1, 1] str

I am very confused why “list” value change to double repeat value. if execute “writeValueAsString” and “readValue” multiple times, each will double for the result

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
medlyingcommented, Dec 4, 2020

@medlying This looks accidental:

    protected static List<String> list = new ArrayList();

since that does not make much sense. If it is intentional, you may want to read a Java tutorial explaining how static members work. @cowtowncoder my jackson-databind version is 2.10.5, in debug model , to execute MAPPER.readValue(jsonString, Test.class) will first go to method : com.fasterxml.jackson.databind.deser.impl.SetterlessProperty#deserializeAndSet it will do this: toModify = _getter.invoke(instance, (Object[]) null); this step will get the ‘list’ value because in my case contains getter method. the second go to com.fasterxml.jackson.databind.deser.std.StringCollectionDeserializer#deserialize(com.fasterxml.jackson.core.JsonParser, com.fasterxml.jackson.databind.DeserializationContext, java.util.Collection<java.lang.String>){ … String value = p.nextTextValue(); if (value != null) { result.add(value); continue; } } then the list will add the text value. so the result is double.So there is no way to avoid this problem except delete getter method?

0reactions
medlyingcommented, Dec 9, 2020

@cowtowncoder cool!! I think this is what I want, Thanks!!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Json.net deserializing list gives duplicate items - Stack Overflow
In my code sample below I serialize an object, containing three types of simple integer lists (property, member var and array). The resulting ......
Read more >
Ho to dynamically deserialize untyped a JSON with list
A JSON list deserializes to a List<Object> . At run time, the contents of the List (the Object instances) will have a type...
Read more >
Duplicate field when serializing: bug or not a bug?
Hello, I have had a request for one of my projects to implement Jackson serialization of my JSON Patch implementation; deserialization was already...
Read more >
How can I store deserialized objects in a List? - CodeRanch
I have created outside of any method (at the top of this class) an ArrayList from the type 'StagingAreaItem', which is a class...
Read more >
JSON Deserialization in Salesforce - Blog - Deadlypenguin
I have been several posts recently on the Developer Boards around JSON ... id="invoiceBlock" columns="1"> <apex:repeat value="{!wrapper.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found