Skip to content

Commit

Permalink
[#5389] Enhance DSLContext.fetchFromJSON() to support new formats
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaseder committed Aug 8, 2016
1 parent c342bc2 commit 0445186
Showing 1 changed file with 63 additions and 48 deletions.
111 changes: 63 additions & 48 deletions jOOQ/src/main/java/org/jooq/impl/JSONReader.java
Expand Up @@ -40,16 +40,17 @@
*/ */
package org.jooq.impl; package org.jooq.impl;


import static org.jooq.impl.Tools.EMPTY_STRING;

import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.Closeable; import java.io.Closeable;
import java.io.IOException; import java.io.IOException;
import java.io.Reader; import java.io.Reader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Map.Entry;


import org.jooq.tools.json.ContainerFactory; import org.jooq.tools.json.ContainerFactory;
import org.jooq.tools.json.JSONParser; import org.jooq.tools.json.JSONParser;
Expand All @@ -60,86 +61,100 @@
* *
* @author Johannes Bühler * @author Johannes Bühler
*/ */
@SuppressWarnings({ "rawtypes", "unchecked" }) @SuppressWarnings({ "unchecked" })
final class JSONReader implements Closeable { final class JSONReader implements Closeable {


private final BufferedReader br; private final BufferedReader br;
private final JSONParser parser; private final JSONParser parser;
private String[] fieldMetaData; private String[] fieldNames;
private Map<String, Integer> fieldIndexes;
private List<String[]> records; private List<String[]> records;


public JSONReader(Reader reader) { JSONReader(Reader reader) {
this.br = new BufferedReader(reader); this.br = new BufferedReader(reader);
this.parser = new JSONParser(); this.parser = new JSONParser();
} }


public List<String[]> readAll() throws IOException { final List<String[]> readAll() throws IOException {
if (this.records != null) { if (records == null) {
return this.records; try {
} LinkedHashMap<String, LinkedList<?>> jsonRoot = getJsonRoot();
try {
LinkedHashMap jsonRoot = getJsonRoot(); readFields(jsonRoot);
readFields(jsonRoot); readRecords(jsonRoot);
records = readRecords(jsonRoot); }
} catch (ParseException ex) {
catch (ParseException ex) { throw new RuntimeException(ex);
throw new RuntimeException(ex); }
} }

return records; return records;
} }


public String[] getFields() throws IOException { final String[] getFields() throws IOException {
if (fieldMetaData == null) { if (fieldNames == null)
readAll(); readAll();
}
return fieldMetaData; return fieldNames;
} }


@Override @Override
public void close() throws IOException { public final void close() throws IOException {
br.close(); br.close();
} }


private List<String[]> readRecords(LinkedHashMap jsonRoot) { private final void readRecords(LinkedHashMap<String, LinkedList<?>> jsonRoot) {
LinkedList jsonRecords = (LinkedList) jsonRoot.get("records"); records = new ArrayList<String[]>();
records = new ArrayList();
for (Object record : jsonRecords) {
LinkedList values = (LinkedList) record;
List<String> v = new ArrayList<String>();
for (Object value : values) {
String asString = value == null ? null : String.valueOf(value);
v.add(asString);
}
records.add(v.toArray(EMPTY_STRING));
}


return records; for (Object record : jsonRoot.get("records")) {
String[] v = new String[fieldNames.length];
int i = 0;

// [#5372] Serialisation mode ARRAY
if (record instanceof LinkedList)
for (Object value : (LinkedList<Object>) record)
v[i++] = value == null ? null : String.valueOf(value);

// [#5372] Serialisation mode OBJECT
else if (record instanceof LinkedHashMap)
for (Entry<String, Object> entry : ((LinkedHashMap<String, Object>) record).entrySet())
v[fieldIndexes.get(entry.getKey())] = entry.getValue() == null ? null : String.valueOf(entry.getValue());

else
throw new IllegalArgumentException("Ill formed JSON : " + jsonRoot);

records.add(v);
}
} }


private LinkedHashMap getJsonRoot() throws IOException, ParseException { private LinkedHashMap<String, LinkedList<?>> getJsonRoot() throws IOException, ParseException {
Object parse = parser.parse(br, new ContainerFactory() { Object parse = parser.parse(br, new ContainerFactory() {
@Override @Override
public LinkedHashMap createObjectContainer() { public LinkedHashMap<String, Object> createObjectContainer() {
return new LinkedHashMap(); return new LinkedHashMap<String, Object>();
} }


@Override @Override
public List createArrayContainer() { public List<Object> createArrayContainer() {
return new LinkedList(); return new LinkedList<Object>();
} }
}); });
return (LinkedHashMap) parse; return (LinkedHashMap<String, LinkedList<?>>) parse;
} }


private void readFields(LinkedHashMap jsonRoot) { private final void readFields(LinkedHashMap<String, LinkedList<?>> jsonRoot) {
if (fieldMetaData != null) { LinkedList<LinkedHashMap<String, String>> fieldEntries =
return; (LinkedList<LinkedHashMap<String, String>>) jsonRoot.get("fields");
}
LinkedList fieldEntries = (LinkedList) jsonRoot.get("fields"); fieldNames = new String[fieldEntries.size()];
fieldMetaData = new String[fieldEntries.size()]; fieldIndexes = new HashMap<String, Integer>();
int i = 0; int i = 0;
for (Object key : fieldEntries) { for (LinkedHashMap<String, String> key : fieldEntries) {
fieldMetaData[i] = (String) ((LinkedHashMap) key).get("name"); String name = key.get("name");

fieldNames[i] = name;
fieldIndexes.put(name, i);

i++; i++;
} }
} }
Expand Down

0 comments on commit 0445186

Please sign in to comment.