Java Code Examples for com.baidu.hugegraph.util.JsonUtil#fromJson()

The following examples show how to use com.baidu.hugegraph.util.JsonUtil#fromJson() . 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: VertexAPI.java    From hugegraph with Apache License 2.0 6 votes vote down vote up
public static Id checkAndParseVertexId(String idValue) {
    if (idValue == null) {
        return null;
    }
    boolean uuid = idValue.startsWith("U\"");
    if (uuid) {
        idValue = idValue.substring(1);
    }
    try {
        Object id = JsonUtil.fromJson(idValue, Object.class);
        return uuid ? Text.uuid((String) id) : HugeVertex.getIdValue(id);
    } catch (Exception e) {
        throw new IllegalArgumentException(String.format(
                  "The vertex id must be formatted as Number/String/UUID" +
                  ", but got '%s'", idValue));
    }
}
 
Example 2
Source File: TextSerializer.java    From hugegraph with Apache License 2.0 6 votes vote down vote up
@Override
public IndexLabel readIndexLabel(HugeGraph graph,
                                 BackendEntry backendEntry) {
    if (backendEntry == null) {
        return null;
    }

    TextBackendEntry entry = this.convertEntry(backendEntry);
    Id id = readId(entry.id());
    String name = JsonUtil.fromJson(entry.column(HugeKeys.NAME),
                                    String.class);
    String baseType = entry.column(HugeKeys.BASE_TYPE);
    String baseValue = entry.column(HugeKeys.BASE_VALUE);
    String indexType = entry.column(HugeKeys.INDEX_TYPE);
    String indexFields = entry.column(HugeKeys.FIELDS);
    String status = entry.column(HugeKeys.STATUS);

    IndexLabel indexLabel = new IndexLabel(graph, id, name);
    indexLabel.baseType(JsonUtil.fromJson(baseType, HugeType.class));
    indexLabel.baseValue(readId(baseValue));
    indexLabel.indexType(JsonUtil.fromJson(indexType, IndexType.class));
    indexLabel.indexFields(readIds(indexFields));
    readUserdata(indexLabel, entry);
    indexLabel.status(JsonUtil.fromJson(status, SchemaStatus.class));
    return indexLabel;
}
 
Example 3
Source File: TraversalUtil.java    From hugegraph with Apache License 2.0 6 votes vote down vote up
private static Number predicateNumber(String value) {
    try {
        return JsonUtil.fromJson(value, Number.class);
    } catch (Exception e) {
        // Try to parse date
        if (e.getMessage().contains("not a valid number") ||
            e.getMessage().contains("Unexpected character ('-'")) {
            try {
                if (value.startsWith("\"")) {
                    value = JsonUtil.fromJson(value, String.class);
                }
                return DateUtil.parse(value).getTime();
            } catch (Exception ignored) {}
        }

        throw new HugeException(
                  "Invalid value '%s', expect a number", e, value);
    }
}
 
Example 4
Source File: MysqlEntryIterator.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
public static PagePosition fromBytes(byte[] bytes) {
    String json = StringEncoding.decode(bytes);
    @SuppressWarnings("unchecked")
    Map<String, Object> columns = JsonUtil.fromJson(json, Map.class);
    Map<HugeKeys, Object> keyColumns = new LinkedHashMap<>();
    for (Map.Entry<String, Object> entry : columns.entrySet()) {
        HugeKeys key = MysqlTable.parseKey(entry.getKey());
        keyColumns.put(key, entry.getValue());
    }
    return new PagePosition(keyColumns);
}
 
Example 5
Source File: TextSerializer.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
private static void readUserdata(SchemaElement schema,
                                 TextBackendEntry entry) {
    // Parse all user data of a schema element
    String userdataStr = entry.column(HugeKeys.USER_DATA);
    @SuppressWarnings("unchecked")
    Map<String, Object> userdata = JsonUtil.fromJson(userdataStr,
                                                     Map.class);
    for (Map.Entry<String, Object> e : userdata.entrySet()) {
        schema.userdata(e.getKey(), e.getValue());
    }
}
 
Example 6
Source File: TextSerializer.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
@Override
public PropertyKey readPropertyKey(HugeGraph graph,
                                   BackendEntry backendEntry) {
    if (backendEntry == null) {
        return null;
    }

    TextBackendEntry entry = this.convertEntry(backendEntry);
    Id id = readId(entry.id());
    String name = JsonUtil.fromJson(entry.column(HugeKeys.NAME),
                                    String.class);
    String dataType = entry.column(HugeKeys.DATA_TYPE);
    String cardinality = entry.column(HugeKeys.CARDINALITY);
    String aggregateType = entry.column(HugeKeys.AGGREGATE_TYPE);
    String properties = entry.column(HugeKeys.PROPERTIES);
    String status = entry.column(HugeKeys.STATUS);

    PropertyKey propertyKey = new PropertyKey(graph, id, name);
    propertyKey.dataType(JsonUtil.fromJson(dataType, DataType.class));
    propertyKey.cardinality(JsonUtil.fromJson(cardinality,
                                              Cardinality.class));
    propertyKey.aggregateType(JsonUtil.fromJson(aggregateType,
                                                AggregateType.class));
    propertyKey.properties(readIds(properties));
    readUserdata(propertyKey, entry);
    propertyKey.status(JsonUtil.fromJson(status, SchemaStatus.class));
    return propertyKey;
}
 
Example 7
Source File: TextSerializer.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
@Override
public EdgeLabel readEdgeLabel(HugeGraph graph,
                               BackendEntry backendEntry) {
    if (backendEntry == null) {
        return null;
    }

    TextBackendEntry entry = this.convertEntry(backendEntry);
    Id id = readId(entry.id());
    String name = JsonUtil.fromJson(entry.column(HugeKeys.NAME),
                                    String.class);
    String sourceLabel = entry.column(HugeKeys.SOURCE_LABEL);
    String targetLabel = entry.column(HugeKeys.TARGET_LABEL);
    String frequency = entry.column(HugeKeys.FREQUENCY);
    String sortKeys = entry.column(HugeKeys.SORT_KEYS);
    String nullablekeys = entry.column(HugeKeys.NULLABLE_KEYS);
    String properties = entry.column(HugeKeys.PROPERTIES);
    String indexLabels = entry.column(HugeKeys.INDEX_LABELS);
    String enableLabelIndex = entry.column(HugeKeys.ENABLE_LABEL_INDEX);
    String status = entry.column(HugeKeys.STATUS);
    String ttl = entry.column(HugeKeys.TTL);
    String ttlStartTime = entry.column(HugeKeys.TTL_START_TIME);

    EdgeLabel edgeLabel = new EdgeLabel(graph, id, name);
    edgeLabel.sourceLabel(readId(sourceLabel));
    edgeLabel.targetLabel(readId(targetLabel));
    edgeLabel.frequency(JsonUtil.fromJson(frequency, Frequency.class));
    edgeLabel.properties(readIds(properties));
    edgeLabel.sortKeys(readIds(sortKeys));
    edgeLabel.nullableKeys(readIds(nullablekeys));
    edgeLabel.indexLabels(readIds(indexLabels));
    edgeLabel.enableLabelIndex(JsonUtil.fromJson(enableLabelIndex,
                                                 Boolean.class));
    readUserdata(edgeLabel, entry);
    edgeLabel.status(JsonUtil.fromJson(status, SchemaStatus.class));
    edgeLabel.ttl(JsonUtil.fromJson(ttl, Long.class));
    edgeLabel.ttlStartTime(readId(ttlStartTime));
    return edgeLabel;
}
 
Example 8
Source File: TextSerializer.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
@Override
public VertexLabel readVertexLabel(HugeGraph graph,
                                   BackendEntry backendEntry) {
    if (backendEntry == null) {
        return null;
    }

    TextBackendEntry entry = this.convertEntry(backendEntry);
    Id id = readId(entry.id());
    String name = JsonUtil.fromJson(entry.column(HugeKeys.NAME),
                                    String.class);
    String idStrategy = entry.column(HugeKeys.ID_STRATEGY);
    String properties = entry.column(HugeKeys.PROPERTIES);
    String primaryKeys = entry.column(HugeKeys.PRIMARY_KEYS);
    String nullableKeys = entry.column(HugeKeys.NULLABLE_KEYS);
    String indexLabels = entry.column(HugeKeys.INDEX_LABELS);
    String enableLabelIndex = entry.column(HugeKeys.ENABLE_LABEL_INDEX);
    String status = entry.column(HugeKeys.STATUS);

    VertexLabel vertexLabel = new VertexLabel(graph, id, name);
    vertexLabel.idStrategy(JsonUtil.fromJson(idStrategy,
                                             IdStrategy.class));
    vertexLabel.properties(readIds(properties));
    vertexLabel.primaryKeys(readIds(primaryKeys));
    vertexLabel.nullableKeys(readIds(nullableKeys));
    vertexLabel.indexLabels(readIds(indexLabels));
    vertexLabel.enableLabelIndex(JsonUtil.fromJson(enableLabelIndex,
                                                   Boolean.class));
    readUserdata(vertexLabel, entry);
    vertexLabel.status(JsonUtil.fromJson(status, SchemaStatus.class));
    return vertexLabel;
}
 
Example 9
Source File: TableSerializer.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
protected <T> T readProperty(PropertyKey pkey, Object value) {
    Class<T> clazz = (Class<T>) pkey.implementClazz();
    T result = JsonUtil.fromJson(value.toString(), clazz);
    if (pkey.cardinality() != Cardinality.SINGLE) {
        Collection<?> values = (Collection<?>) result;
        List<Object> newValues = new ArrayList<>(values.size());
        for (Object v : values) {
            newValues.add(JsonUtil.castNumber(v, pkey.dataType().clazz()));
        }
        result = (T) newValues;
    }
    return result;
}
 
Example 10
Source File: TextBackendEntry.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
public void eliminate(TextBackendEntry entry) {
    for (Entry<String, String> col : entry.columns.entrySet()) {
        String newValue = col.getValue();
        String oldValue = this.column(col.getKey());

        // TODO: use more general method
        if (col.getKey().startsWith(HugeType.PROPERTY.string()) ||
            col.getKey().startsWith(HugeType.EDGE_OUT.string()) ||
            col.getKey().startsWith(HugeType.EDGE_IN.string())) {
            this.columns.remove(col.getKey());
            continue;
        }

        // TODO: use more general method
        if (!col.getKey().endsWith(HugeKeys.ELEMENT_IDS.string())) {
            continue;
        }

        // TODO: ensure the old value is a list and json format (for index)
        List<Object> values = new ArrayList<>();
        @SuppressWarnings("unchecked")
        List<Object> oldValues = JsonUtil.fromJson(oldValue, List.class);
        @SuppressWarnings("unchecked")
        List<Object> newValues = JsonUtil.fromJson(newValue, List.class);
        values.addAll(oldValues);
        values.removeAll(newValues);
        // Update the old value
        this.column(col.getKey(), JsonUtil.toJson(values));
    }
}
 
Example 11
Source File: TextBackendEntry.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
public void append(TextBackendEntry entry) {
    for (Entry<String, String> col : entry.columns.entrySet()) {
        String newValue = col.getValue();
        String oldValue = this.column(col.getKey());

        // TODO: use more general method
        if (col.getKey().startsWith(HugeType.PROPERTY.string())) {
            this.columns.put(col.getKey(), col.getValue());
            continue;
        }

        // TODO: use more general method
        if (!col.getKey().endsWith(HugeKeys.ELEMENT_IDS.string())) {
            continue;
        }

        // TODO: ensure the old value is a list and json format (for index)
        if (oldValue.equals("[]")) {
            this.column(col.getKey(), newValue);
            continue;
        }
        List<Object> values = new ArrayList<>();
        @SuppressWarnings("unchecked")
        List<Object> oldValues = JsonUtil.fromJson(oldValue, List.class);
        @SuppressWarnings("unchecked")
        List<Object> newValues = JsonUtil.fromJson(newValue, List.class);
        values.addAll(oldValues);
        values.addAll(newValues);
        // Update the old value
        this.column(col.getKey(), JsonUtil.toJson(values));
    }
}
 
Example 12
Source File: BinarySerializer.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
private void readUserdata(SchemaElement schema) {
    // Parse all user data of a schema element
    byte[] userdataBytes = column(HugeKeys.USER_DATA);
    String userdataStr = StringEncoding.decode(userdataBytes);
    @SuppressWarnings("unchecked")
    Map<String, Object> userdata = JsonUtil.fromJson(userdataStr,
                                                     Map.class);
    for (Map.Entry<String, Object> e : userdata.entrySet()) {
        schema.userdata(e.getKey(), e.getValue());
    }
}
 
Example 13
Source File: TraversalUtil.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static <V> List<V> predicateArgs(String value) {
    try {
        return JsonUtil.fromJson("[" + value + "]", List.class);
    } catch (Exception e) {
        throw new HugeException(
                  "Invalid value '%s', expect a list", e, value);
    }
}
 
Example 14
Source File: TraversalUtil.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static <V> V predicateArg(String value) {
    try {
        return (V) JsonUtil.fromJson(value, Object.class);
    } catch (Exception e) {
        throw new HugeException(
                  "Invalid value '%s', expect a single value", e, value);
    }
}
 
Example 15
Source File: RolePermission.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
public static RolePermission fromJson(Object json) {
    RolePermission role;
    if (json instanceof String) {
        role = JsonUtil.fromJson((String) json, RolePermission.class);
    } else {
        // Optimized json with RolePermission object
        E.checkArgument(json instanceof RolePermission,
                        "Invalid role value: %s", json);
        role = (RolePermission) json;
    }
    return role;
}
 
Example 16
Source File: HugeResource.java    From hugegraph with Apache License 2.0 4 votes vote down vote up
public static List<HugeResource> parseResources(String resources) {
    TypeReference<?> type = new TypeReference<List<HugeResource>>() {};
    return JsonUtil.fromJson(resources, type);
}
 
Example 17
Source File: HugeResource.java    From hugegraph with Apache License 2.0 4 votes vote down vote up
public static HugeResource parseResource(String resource) {
    return JsonUtil.fromJson(resource, HugeResource.class);
}
 
Example 18
Source File: JsonGraph.java    From hugegraph-tools with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public Map<String, Object> properties() {
    return JsonUtil.fromJson(this.properties, Map.class);
}
 
Example 19
Source File: HugeAuthenticator.java    From hugegraph with Apache License 2.0 4 votes vote down vote up
public static RoleAction fromJson(String json) {
    return JsonUtil.fromJson(json, RoleAction.class);
}
 
Example 20
Source File: BaseUnitTest.java    From hugegraph-client with Apache License 2.0 4 votes vote down vote up
public static <T> T deserialize(String json, Class<T> clazz) {
    return JsonUtil.fromJson(json, clazz);
}