com.jstarcraft.core.common.conversion.json.JsonUtility Java Examples
The following examples show how to use
com.jstarcraft.core.common.conversion.json.JsonUtility.
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 |
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 |
@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: JsonContentCodec.java From jstarcraft-core with Apache License 2.0 | 6 votes |
@Override public Object decode(Type type, byte[] content) { try { if (content.length == 0) { return null; } Specification specification = Specification.getSpecification(type); if (specification == Specification.TYPE) { currentTypes.set(type); Type value = typeConverter.readValue(content, Type.class); currentTypes.remove(); return value; } else { return typeConverter.readValue(content, JsonUtility.type2Java(type)); } } catch (Exception exception) { String message = "JSON解码异常"; LOGGER.error(message, exception); throw new CodecException(message, exception); } }
Example #4
Source File: JsonContentCodec.java From jstarcraft-core with Apache License 2.0 | 6 votes |
@Override public Object decode(Type type, InputStream stream) { try { Specification specification = Specification.getSpecification(type); if (specification == Specification.TYPE) { currentTypes.set(type); Type value = typeConverter.readValue(stream, Type.class); currentTypes.remove(); return value; } else { return typeConverter.readValue(stream, JsonUtility.type2Java(type)); } } catch (Exception exception) { String message = "JSON解码异常"; LOGGER.error(message, exception); throw new CodecException(message, exception); } }
Example #5
Source File: ClassUtilityTestCase.java From jstarcraft-core with Apache License 2.0 | 6 votes |
@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 #6
Source File: DetectionTrie.java From jstarcraft-nlp with Apache License 2.0 | 5 votes |
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 #7
Source File: ConfigurationTestCase.java From jstarcraft-rns with Apache License 2.0 | 5 votes |
@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 #8
Source File: JsonFormatAdapter.java From jstarcraft-core with Apache License 2.0 | 5 votes |
@Override public <E> Iterator<E> iterator(Class<E> clazz, InputStream stream) { try { JavaType type = JsonUtility.type2Java(TypeUtility.parameterize(LinkedList.class, clazz)); List<E> list = TYPE_CONVERTER.readValue(stream, type); return list.iterator(); } catch (Exception exception) { throw new StorageException("遍历JSON异常", exception); } }
Example #9
Source File: YamlFormatAdapter.java From jstarcraft-core with Apache License 2.0 | 5 votes |
@Override public <E> Iterator<E> iterator(Class<E> clazz, InputStream stream) { try { JavaType type = JsonUtility.type2Java(TypeUtility.parameterize(LinkedList.class, clazz)); List<E> list = TYPE_CONVERTER.readValue(stream, type); return list.iterator(); } catch (Exception exception) { throw new StorageException("遍历YAML异常", exception); } }
Example #10
Source File: JsonType.java From jstarcraft-core with Apache License 2.0 | 5 votes |
@Override public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index, SharedSessionContractImplementor session) throws HibernateException, SQLException { if (value != null) { String json; synchronized (value) { json = value instanceof String ? (String) value : JsonUtility.object2String(value); } preparedStatement.setString(index, json); } else { preparedStatement.setNull(index, TextType.INSTANCE.sqlType()); } }
Example #11
Source File: ZipJsonType.java From jstarcraft-core with Apache License 2.0 | 5 votes |
@Override public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index, SharedSessionContractImplementor session) throws HibernateException, SQLException { if (value != null) { String json; synchronized (value) { json = JsonUtility.object2String(value); } byte[] bytes = json.getBytes(StringUtility.CHARSET); byte[] zip = PressUtility.zip(bytes, 5); ByteArrayInputStream inputStream = new ByteArrayInputStream(zip); preparedStatement.setBinaryStream(index, inputStream); } else { preparedStatement.setNull(index, BlobType.INSTANCE.sqlType()); } }
Example #12
Source File: CsvUtilityTestCase.java From jstarcraft-core with Apache License 2.0 | 5 votes |
@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())); }
Example #13
Source File: JsonIdConverter.java From jstarcraft-core with Apache License 2.0 | 4 votes |
@Override public String convert(Type type, Object id) { return JsonUtility.object2String(id); }