Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generating String similar to JSON without quotes. #715

Closed
cemo opened this issue Mar 2, 2015 · 5 comments
Closed

Generating String similar to JSON without quotes. #715

cemo opened this issue Mar 2, 2015 · 5 comments

Comments

@cemo
Copy link

cemo commented Mar 2, 2015

How can I configure Jackson to generate text similar to JSON without quotes? I will use this as a String for aesthetic purposes and set inside another Map<String, String>. What is the easiest way to create?

@cowtowncoder
Copy link
Member

I think I need bit more information on what you mean by "similar to JSON without quotes".

If you mean 'raw' output ("just include this String verbatim with no modifications in output"), that would be via streaming API's writeRaw() and/or writeRawValue() (former adds no decorations whatsoever; latter adds necessary separators like commas).
And at databind level it can be done with @JsonRawValue annotation.

@cemo
Copy link
Author

cemo commented Mar 2, 2015

      ObjectMapper objectMapper = new ObjectMapper();
      HashMap<String, Object> map = new HashMap<String, Object>();
      map.put("a", 1);
      map.put("b", "hello");
      System.out.println(objectMapper.writeValueAsString(map));

This is generating:

{"a":1,"b":"hello"}

I want this (please note that hello is also without quote):

{a:1,b:hello}

@cowtowncoder
Copy link
Member

Ok. It is possible to force use of unquoted property names with JsonGenerator.Feature.QUOTE_FIELD_NAMES (defaults to true, disable).
There is no setting for doing that to values: if you really want that, writing a custom unquoted-String serializer would be easy; it'd just use writeRawValue() instead of writeString().

@Abhishek-P
Copy link

Abhishek-P commented Sep 22, 2017

In stringSerializer with a WriterBasedJsonGenerator writeString() adds quotation marks " at each end of the string. Since we already know the walue being passed is a string, do we need to add an extra pair of quotation marks ??
And if yes How can I avoid that...
om.fasterxml.jackson.core/jackson-core/2.8.7/b7aa166b68d41d9e5aabf81d5783c61f773cefc7/jackson-core-2.8.7-sources.jar!/com/fasterxml/jackson/core/json/WriterBasedJsonGenerator.java:355

@cowtowncoder
Copy link
Member

@Abhishek-P please send usage questions on mailing lists. Adding comments at the end of closed issues is not going to do you much good.

Also make sure you understand role of JsonGenerator: it will output JSON Strings that are quoted and escaped. If you don't want that don't use writeString().

@FasterXML FasterXML locked and limited conversation to collaborators Sep 22, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants