Is there a way to tell Jackson-Objectmapper to parse a comma-separated String to a String[]?
Jackson has module jackson-dataformat-csv for parsing CSV data.
Just use its CsvMapper instead of plain ObjectMapper. For example:
String s = "aa,bbb,c,dd";
ObjectMapper objectMapper = new CsvMapper();
String[] array = objectMapper.readValue(s, String[].class);
Related
I have converted Json Response to Jackson Object Mapper and CSV to CSV Mapper Object. How should I compare the two? Is this the correct approach?
public void test_CSVtoObject() throws KronosCoreCommonException, IOException {
File csvFile = new File("C:\\Users\\Desktop\\PayrollCSV.csv");
CsvMapper csvMapper = new CsvMapper();
CsvSchema csvSchema = csvMapper
.typedSchemaFor(PayrollExtractCSV.class)
.withHeader()
.withColumnSeparator(',')
.withComments();
MappingIterator<PayrollExtractCSV> mappingIter = csvMapper
.readerWithTypedSchemaFor(PayrollExtractCSV.class)
.with(csvSchema)
.readValues(csvFile);
List<PayrollExtractCSV> mapping = mappingIter.readAll();
}
public void test_JSONtoObject() throws KronosCoreCommonException, JsonMappingException, JsonProcessingException {
List<String> personNumberList = new ArrayList<>();
personNumberList.add("1619889490630");
APIResponse aggregationResponse = payRollStagingAPIObject.runAggregationAPI("T01", personNumberList);
ObjectMapper mapper = new ObjectMapper();
AggregationAPI[] aggregationAPI = mapper.readValue(aggregationResponse.getResponseAsString(), AggregationAPI[].class);
mapper.writeValueAsString(aggregationAPI);
}
Because of the input size, you should beware of collecting all of the data to memory, which might cause OutOfMemoryException.
For both sources, you should read the data line by line while discarding old data as soon as you are done processing it.
It seems like mappingIter.next() will do the trick, and you can use Jackson Streaming API for the JSON source.
You already have a PayrollExtractCSV class, so you can construct objects from both sources and compare them, one by one.
Normally to print out request body i using ObjectMapper, but this way removing space and printing object to string in one line, example :
if i send request body like this :
{
"Header" : "value"
}
and i using objectMapper to print that object
ObjectMapper mapper = new ObjectMapper();
mapper.writeValueAsString(requestBody)
the out put is like this :
{"Header":"value"}
how to print Original request body without any modification ?
I'm not sure if you can print it in it's original form but you can pretty print it.
With Jackson's Object Mapper, you can do something like:
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(body);
System.out.println(json);
or
ObjectMapper mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
String json = mapper.writeValueAsString(body);
System.out.println(json);
I don't think you can do this without using any framework.
But you can use the gson for this if you enable the prettyPrinting option. Example:
Gson gson = new GsonBuilder()
.setPrettyPrinting()
.create();
prettyPrintedString = gson.toJson(requestBody, Object.class);
I have a JSON stored in a string.
String data = "{code: '0', distCode: '123'}";
I need to get the values of code, distCode. But when I try to parse it as below
JSONParser parser = new JSONParser();
JSONObject Details = (JSONObject) parser.parse(data);
Unexpected character (c) at position 2 exception is thrown.
I am sure it is because of unquoted keys in the string. How to parse the string into an JSON object using org.json.simple library?
Could not find way to achieve it using org.json.simple library. Finally done it using jackson libraries.
String data = "{code: '0', distCode: '123'}";
ObjectMapper mapper = new ObjectMapper();
mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
Map<String, String> Shop_Details = mapper.readValue(data), Map.class);
I am using writeValueAsString of the ObjectMapper . However, it's giving me a Java String representation so I get:
{"network_id":5000370004610700049753}
instead of
"{\"network_id\":5000370004610700049753}"
which is failing for other services when deserializing. How do I get this kind of serialization with the ObjectMapper?
To get the second result, send it through the ObjectMapper again.
Map<String, Object> data = new HashMap<>();
data.put("network_id", new BigInteger("5000370004610700049753"));
ObjectMapper objectMapper = new ObjectMapper();
String plainJson = objectMapper.writeValueAsString(data);
System.out.println(plainJson);
String encodedJson = objectMapper.writeValueAsString(plainJson);
System.out.println(encodedJson);
Output
{"network_id":5000370004610700049753}
"{\"network_id\":5000370004610700049753}"
I have the following code:
public static void postHttpStream(ArrayListMultimap<String, String> fcmbuildProperties){
HttpClient client = new HttpClient();
Gson gson = new Gson();
System.out.println(fcmbuildProperties);
String jsonString = gson.toJson(fcmbuildProperties);
System.out.println(jsonString);
}
where fcmbuildProperties is an ArrayListMultimap. I try to convert that to JSON here: String jsonString = gson.toJson(fcmbuildProperties); But this returns an empty array. What do I need to do instead?
This is the input that fcmbuildProperties contain : {build.name=[test_project], build.timestamp=[1425600727488], build.number=[121]}
I need to convert this to Json. with key/values.
Use ArrayListMultimap#asMap()
String jsonString = gson.toJson(fcmbuildProperties.asMap());
Gson considers ArrayListMultimap as a Map and ignores its internal state which actually manages the multimap. asMap returns a corresponding Map instance which you can serialize as expected.