javachroniclechronicle-mapchronicle-byteschronicle-wire

How to effectively parse and store data to Chronicle Map


I'm trying to figure out shortest and effective path to parse values from json and store some of them into chronicle map.

My current solution:

String data = ...;
ChronicleMap<CharSequence, Long> map = ...;
JSONWire wire = new JSONWire(Bytes.allocateElasticDirect(128));
wire.bytes().append(data);
long value = wire.read("fieldName").readLong();
map.put("key", value);

Can I escape somehow reading to long and save bytes to map, assuming that this is long?


Solution

  • If you are looking for the first occurrence of the field name and want the value after it, I would parse it manually. JSOWire assumes you want to read the whole message.

    String field = "\"fieldName\":";
    int pos = data.indexOf(field);
    if (pos >= 0) {
       int end = data.indexOf(",", pos + field.length());
       long value = Long.parseLong(data.substring(pos + field.length(), end));
       // use the value
    }