Java Code Examples for com.jstarcraft.core.common.conversion.json.JsonUtility#string2Object()

The following examples show how to use com.jstarcraft.core.common.conversion.json.JsonUtility#string2Object() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DetectionPattern.java    From jstarcraft-nlp with Apache License 2.0 6 votes vote down vote up
public static final Map<String, DetectionPattern> loadPatterns(InputStream stream) {
    try (DataInputStream buffer = new DataInputStream(stream)) {
        Map<String, DetectionPattern> detections = new HashMap<>();
        byte[] data = new byte[buffer.available()];
        buffer.readFully(data);
        String json = new String(data, StringUtility.CHARSET);
        // 兼容\x转义ASCII字符
        json = StringUtility.unescapeJava(json);
        Type type = TypeUtility.parameterize(LinkedHashMap.class, String.class, String.class);
        LinkedHashMap<String, String> regulations = JsonUtility.string2Object(json, type);

        for (Entry<String, String> scriptTerm : regulations.entrySet()) {
            String script = scriptTerm.getKey();
            String regulation = scriptTerm.getValue();
            Pattern pattern = Pattern.compile(regulation, Pattern.MULTILINE);
            DetectionPattern detection = new DetectionPattern(script, pattern);
            detections.put(script, detection);
        }
        return Collections.unmodifiableMap(detections);
    } catch (Exception exception) {
        throw new IllegalArgumentException(exception);
    }
}
 
Example 2
Source File: JsonType.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
@Override
public Object nullSafeGet(ResultSet resultSet, String[] names, SharedSessionContractImplementor session, Object object) throws HibernateException, SQLException {
    if (object == null) {
        return null;
    }
    String json = resultSet.getString(names[0]);
    String columnName = getColumnName(resultSet, names[0]);
    String fieldName = getFieldName(object.getClass(), columnName);
    Type type = null;
    try {
        type = ReflectionUtility.findField(object.getClass(), fieldName).getGenericType();
    } catch (Exception exception) {
        throw new StorageAccessException(exception);
    }
    Object value = JsonUtility.string2Object(json, type);
    return value;
}
 
Example 3
Source File: ClassUtilityTestCase.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
@Test
public void test() throws Exception {
    // 模拟类不在ClassLoader的情况
    String name = "com.jstarcraft.core.common.compilation.MockTask";
    String path = "MockTask.clazz";
    Map<String, byte[]> bytes = new HashMap<>();
    try (InputStream stream = ClassUtilityTestCase.class.getResourceAsStream(path); DataInputStream buffer = new DataInputStream(stream)) {
        byte[] data = new byte[buffer.available()];
        buffer.readFully(data);
        bytes.put(name, data);
        Class<?>[] classes = ClassUtility.bytes2Classes(bytes);
        Type type = TypeUtility.parameterize(classes[0], String.class);
        Callable<String> task = JsonUtility.string2Object("{\"value\":\"birdy\"}", type);
        Assert.assertEquals("birdy", task.call());
        Assert.assertEquals(bytes, ClassUtility.classes2Bytes(classes));
    }
}
 
Example 4
Source File: DetectionTrie.java    From jstarcraft-nlp with Apache License 2.0 5 votes vote down vote up
public static final Map<String, Set<DetectionTrie>> loadTries(InputStream stream) {
    try (DataInputStream buffer = new DataInputStream(stream)) {
        Map<String, Set<DetectionTrie>> detections = new HashMap<>();
        byte[] data = new byte[buffer.available()];
        buffer.readFully(data);
        String json = new String(data, StringUtility.CHARSET);
        // 兼容\x转义ASCII字符
        json = StringUtility.unescapeJava(json);
        Type type = TypeUtility.parameterize(LinkedHashMap.class, String.class, String.class);
        type = TypeUtility.parameterize(LinkedHashMap.class, String.class, LinkedHashMap.class);
        LinkedHashMap<String, LinkedHashMap<String, String>> dictionaries = JsonUtility.string2Object(json, type);

        for (Entry<String, LinkedHashMap<String, String>> scriptTerm : dictionaries.entrySet()) {
            Set<DetectionTrie> tries = new HashSet<>();
            String script = scriptTerm.getKey();
            LinkedHashMap<String, String> languages = scriptTerm.getValue();
            for (Entry<String, String> languageTerm : languages.entrySet()) {
                String language = languageTerm.getKey();
                String[] dictionary = languageTerm.getValue().split("\\|");
                int weight = 0;
                TreeMap<String, Integer> tree = new TreeMap<>();
                for (String word : dictionary) {
                    tree.put(word, weight++);
                }
                DoubleArrayTrie<Integer> trie = new DoubleArrayTrie<>(tree);
                DetectionTrie detection = new DetectionTrie(language, trie);
                tries.add(detection);
            }
            detections.put(script, Collections.unmodifiableSet(tries));
        }
        return Collections.unmodifiableMap(detections);
    } catch (Exception exception) {
        throw new IllegalArgumentException(exception);
    }
}
 
Example 5
Source File: ConfigurationTestCase.java    From jstarcraft-rns with Apache License 2.0 5 votes vote down vote up
@Test
public void testConfigurationJson() {
    LinkedList<KeyValue<String, Class<?>>> left = new LinkedList<>();
    left.add(new KeyValue<>("user", String.class));
    left.add(new KeyValue<>("item", int.class));
    left.add(new KeyValue<>("score", double.class));
    left.add(new KeyValue<>("word", String.class));
    String json = JsonUtility.object2String(left);
    Type type = TypeUtility.parameterize(KeyValue.class, String.class, Class.class);
    type = TypeUtility.parameterize(LinkedList.class, type);
    LinkedList<KeyValue<String, Class<?>>> right = JsonUtility.string2Object(json, type);
    assertEquals(left, right);
}
 
Example 6
Source File: CsvUtilityTestCase.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Test
public void testPerformance() {
    Instant now = null;
    int times = 100000;
    CsvObject object = CsvObject.instanceOf(0, "birdy", "hong", 10, Instant.now(), CsvEnumeration.TERRAN);
    // CSV与JSON各自都装载一次
    String csv = CsvUtility.object2String(object, CsvObject.class);
    String json = JsonUtility.object2String(object);
    String message = StringUtility.format("CSV格式化与JSON格式化对比:\nCSV:{}\nJSON:{}", csv, json);
    logger.debug(message);

    now = Instant.now();
    for (int index = 0; index < times; index++) {
        CsvUtility.object2String(CsvObject.instanceOf(index, "birdy" + index, "hong" + index, index % 10, Instant.now(), CsvEnumeration.TERRAN), CsvObject.class);
    }
    logger.debug(StringUtility.format("CSV编码{}次一共消耗{}毫秒.", times, System.currentTimeMillis() - now.toEpochMilli()));

    now = Instant.now();
    for (int index = 0; index < times; index++) {
        JsonUtility.object2String(CsvObject.instanceOf(index, "birdy" + index, "hong" + index, index % 10, Instant.now(), CsvEnumeration.TERRAN));
    }
    logger.debug(StringUtility.format("JSON编码{}次一共消耗{}毫秒.", times, System.currentTimeMillis() - now.toEpochMilli()));

    now = Instant.now();
    for (int index = 0; index < times; index++) {
        csv = CsvUtility.object2String(CsvObject.instanceOf(index, "birdy" + index, "hong" + index, index % 10, Instant.now(), CsvEnumeration.TERRAN), CsvObject.class);
        CsvUtility.string2Object(csv, CsvObject.class);
    }
    logger.debug(StringUtility.format("CSV解码{}次一共消耗{}毫秒.", times, System.currentTimeMillis() - now.toEpochMilli()));

    now = Instant.now();
    for (int index = 0; index < times; index++) {
        json = JsonUtility.object2String(CsvObject.instanceOf(index, "birdy" + index, "hong" + index, index % 10, Instant.now(), CsvEnumeration.TERRAN));
        JsonUtility.string2Object(json, CsvObject.class);
    }
    logger.debug(StringUtility.format("JSON解码{}次一共消耗{}毫秒.", times, System.currentTimeMillis() - now.toEpochMilli()));
}