com.scudata.util.JSONUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of esproc Show documentation
Show all versions of esproc Show documentation
SPL(Structured Process Language) A programming language specially for structured data computing.
package com.scudata.util;
import java.util.Date;
import com.scudata.array.IArray;
import com.scudata.common.Escape;
import com.scudata.common.Sentence;
import com.scudata.common.StringUtils;
import com.scudata.dm.BaseRecord;
import com.scudata.dm.DataStruct;
import com.scudata.dm.Record;
import com.scudata.dm.Sequence;
import com.scudata.dm.Table;
import java.util.ArrayList;
public final class JSONUtil {
private static int scanQuotation(char []chars, int start, int end) {
for (; start <= end; ++start) {
if (chars[start] == '"') {
return start;
} else if (chars[start] == '\\') {
start++;
}
}
return -1;
}
private static int indexOf(char []chars, int start, int end, char c) {
for (; start <= end; ++start) {
if (chars[start] == c) return start;
switch (chars[start]) {
case '[':
start = indexOf(chars, start + 1, end, ']');
if (start < 0) {
return -1;
}
break;
case '{':
start = indexOf(chars, start + 1, end, '}');
if (start < 0) {
return -1;
}
break;
case '"':
start = scanQuotation(chars, start + 1, end);
if (start < 0) {
return -1;
}
break;
case '\\':
start++;
break;
}
}
return -1;
}
/**
* v1,v2...
* @param chars
* @param start ֵ????ʼλ?ã?????
* @param end ֵ?Ľ???λ?ã?????
* @return Sequence
*/
private static Sequence parseSequence(char []chars, int start, int end, String opt) {
// ????ǰ??Ŀհ?
for (; start <= end && Character.isWhitespace(chars[start]); ++start) {
}
Sequence sequence = new Sequence();
while (start <= end) {
int index = indexOf(chars, start, end, ',');
if (index < 0) {
Object value = parseJSON(chars, start, end, opt);
sequence.add(value);
break;
} else {
Object value = parseJSON(chars, start, index - 1, opt);
sequence.add(value);
// ???????ź?Ŀհ?
for (start = index + 1; start <= end && Character.isWhitespace(chars[start]); ++start) {
}
if (start > end) {
sequence.add(null);
break;
}
}
}
if (!sequence.isPmt()) {
return sequence;
}
DataStruct ds = sequence.dataStruct();
if (ds != null) {
// ????Ǵ???????תΪ???
int len = sequence.length();
Table table = new Table(ds, len);
IArray memes = table.getMems();
for (int i = 1; i <= len; ++i) {
Record r = (Record)sequence.getMem(i);
r.setDataStruct(ds);
memes.add(r);
}
return table;
} else if (opt != null && opt.indexOf('t') != -1) {
// ?????Ľṹ???ж???
ArrayList nameList = new ArrayList();
int len = sequence.length();
for (int i = 1; i <= len; ++i) {
BaseRecord r = (BaseRecord)sequence.getMem(i);
if (r != null) {
String []names = r.getFieldNames();
for (String name : names) {
if (!nameList.contains(name)) {
nameList.add(name);
}
}
}
}
String []names = new String[nameList.size()];
nameList.toArray(names);
ds = new DataStruct(names);
Table table = new Table(ds, len);
for (int i = 1; i <= len; ++i) {
BaseRecord nr = table.newLast();
BaseRecord r = (BaseRecord)sequence.getMem(i);
if (r != null) {
int f = 0;
names = r.getFieldNames();
for (String name : names) {
int nf = ds.getFieldIndex(name);
nr.setNormalFieldValue(nf, r.getNormalFieldValue(f++));
}
}
}
return table;
} else {
return sequence;
}
}
/**
* n1:v1,n2:v2...
* @param chars
* @param start ֵ????ʼλ?ã?????
* @param end ֵ?Ľ???λ?ã?????
* @return BaseRecord
*/
private static BaseRecord parseRecord(char []chars, int start, int end, String opt) {
if (start > end) {
return null;
}
ArrayList nameList = new ArrayList();
ArrayList
© 2015 - 2024 Weber Informatics LLC | Privacy Policy