0

I tried to to use json4s native library. Here is how I use it:

Builder b = new JsonMessageBuilder();
Map<String, List<Object>> m2 = new HashMap<>();
m2.put("e", Arrays.asList(new StringBuilder(123)));
b.message(JavaConverters.mapAsScalaMapConverter(m2).asScala());

public static class Test{
    @Override
    public String toString(){
        return "test";
    }
}

Where

class JsonMessageBuilder{
    def message[A <: AnyRef](a: A) = println(Serialization.write(a))
}

But the code prints this:

{"e":[{}]}

How to force serialization of toString result in a map? I want to have

{"e":["test"]}
user3663882
  • 6,957
  • 10
  • 51
  • 92

1 Answers1

1

Most likely, the issue is that you are not instantiating your StringBuilder correctly i.e. not enclosing the argument 123 in quotes. Thats why you are getting an empty stringbuilder. The serialization is happening correctly as the {"e":[{}]} is printed. Try this:

m2.put("e", Arrays.asList(new StringBuilder("123")));
Samar
  • 2,091
  • 1
  • 15
  • 18