com.weavechain.core.encoding.RecordsJSONAdapter Maven / Gradle / Ivy
package com.weavechain.core.encoding;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.JsonReader;
import com.squareup.moshi.JsonWriter;
import com.weavechain.core.data.ConvertUtils;
import com.weavechain.core.data.DataLayout;
import com.weavechain.core.data.DataType;
import com.weavechain.core.data.Records;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.CopyOnWriteArrayList;
public class RecordsJSONAdapter extends JsonAdapter {
private DataLayout dataLayout;
public RecordsJSONAdapter withLayout(DataLayout dataLayout) {
this.dataLayout = dataLayout;
return this;
}
@Override
public Records fromJson(JsonReader reader) throws IOException {
reader.beginObject();
String tableName = null;
CopyOnWriteArrayList integrityPairs = null;
List> items = null;
String tag;
do {
tag = reader.nextName();
if ("table".equals(tag)) {
tableName = reader.nextString();
} else if ("integrity".equals(tag)) {
integrityPairs = readIntegrityPairs(reader);
} else if ("items".equals(tag)) {
items = readItems(reader);
}
} while (tag != null && reader.peek() != JsonReader.Token.END_OBJECT);
reader.endObject();
Records r = new Records(tableName, items, null, null);
r.setIntegrity(integrityPairs);
return r;
}
@Override
public void toJson(JsonWriter writer, Records records) throws IOException {
writer.beginObject();
// TABLE -------------------------------------------------------------------------
writer.name("table");
writer.value(records.getTable());
// INTEGRITY ---------------------------------------------------------------------
if (records.getIntegrity() != null) {
writer.name("integrity");
writer.beginArray();
for (Records.IntegrityPair integrityPair : records.getIntegrity()) {
writeIntegrityPairs(writer, integrityPair);
}
writer.endArray();
}
// ITEMS -------------------------------------------------------------------------
writer.name("items");
writer.beginArray();
for (List