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

The following examples show how to use com.baidu.hugegraph.util.JsonUtil#toJson() . 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: JsonSerializer.java    From hugegraph with Apache License 2.0 6 votes vote down vote up
@Override
public String writePaths(String name, Collection<HugeTraverser.Path> paths,
                         boolean withCrossPoint,
                         Iterator<Vertex> vertices) {
    List<Map<String, Object>> pathList = new ArrayList<>(paths.size());
    for (HugeTraverser.Path path : paths) {
        pathList.add(path.toMap(withCrossPoint));
    }

    Map<String, Object> results;
    if (vertices == null) {
        results = ImmutableMap.of(name, pathList);
    } else {
        results = ImmutableMap.of(name, pathList, "vertices", vertices);
    }
    return JsonUtil.toJson(results);
}
 
Example 2
Source File: MetricsAPI.java    From hugegraph with Apache License 2.0 6 votes vote down vote up
@GET
@Timed
@Path("backend")
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed("admin")
public String backend(@Context GraphManager manager) {
    Map<String, Map<String, Object>> results = InsertionOrderUtil.newMap();
    for (String graph : manager.graphs()) {
        HugeGraph g = manager.graph(graph);
        Map<String, Object> metrics = InsertionOrderUtil.newMap();
        metrics.put(BackendMetrics.BACKEND, g.backend());
        try {
            metrics.putAll(g.metadata(null, "metrics"));
        } catch (Throwable e) {
            metrics.put(BackendMetrics.EXCEPTION, e.toString());
            LOG.debug("Failed to get backend metrics", e);
        }
        results.put(graph, metrics);
    }
    return JsonUtil.toJson(results);
}
 
Example 3
Source File: MysqlSerializer.java    From hugegraph with Apache License 2.0 6 votes vote down vote up
@Override
protected void parseProperties(HugeElement element,
                               TableBackendEntry.Row row) {
    String properties = row.column(HugeKeys.PROPERTIES);
    // Query edge will wraped by a vertex, whose properties is empty
    if (properties.isEmpty()) {
        return;
    }

    @SuppressWarnings("unchecked")
    Map<String, Object> props = JsonUtil.fromJson(properties, Map.class);
    for (Map.Entry<String, Object> prop : props.entrySet()) {
        /*
         * The key is string instead of int, because the key in json
         * must be string
         */
        Id pkeyId = this.toId(Long.valueOf(prop.getKey()));
        String colJson = JsonUtil.toJson(prop.getValue());
        this.parseProperty(pkeyId, colJson, element);
    }
}
 
Example 4
Source File: JsonUtilTest.java    From hugegraph with Apache License 2.0 6 votes vote down vote up
@Test
public void testSerializePropertyKey() {
    FakeObjects fakeObject = new FakeObjects();
    PropertyKey name = fakeObject.newPropertyKey(IdGenerator.of(1), "name");
    String json = JsonUtil.toJson(name);
    Assert.assertEquals("{\"id\":1,\"name\":\"name\"," +
                        "\"data_type\":\"TEXT\"," +
                        "\"cardinality\":\"SINGLE\"," +
                        "\"aggregate_type\":\"NONE\"," +
                        "\"properties\":[],\"status\":\"CREATED\"," +
                        "\"user_data\":{}}", json);

    PropertyKey rate = fakeObject.newPropertyKey(IdGenerator.of(2), "rate",
                                                 DataType.INT,
                                                 Cardinality.LIST);
    json = JsonUtil.toJson(rate);
    Assert.assertEquals("{\"id\":2,\"name\":\"rate\"," +
                        "\"data_type\":\"INT\",\"cardinality\":\"LIST\"," +
                        "\"aggregate_type\":\"NONE\",\"properties\":[]," +
                        "\"status\":\"CREATED\",\"user_data\":{}}", json);
}
 
Example 5
Source File: JsonUtilTest.java    From hugegraph with Apache License 2.0 6 votes vote down vote up
@Test
public void testSerializeVertexLabel() {
    FakeObjects fakeObject = new FakeObjects();
    PropertyKey name = fakeObject.newPropertyKey(IdGenerator.of(1), "name");
    PropertyKey age = fakeObject.newPropertyKey(IdGenerator.of(2), "age",
                                                 DataType.INT,
                                                 Cardinality.SINGLE);
    PropertyKey city = fakeObject.newPropertyKey(IdGenerator.of(3), "city");

    VertexLabel vl = fakeObject.newVertexLabel(IdGenerator.of(1), "person",
                                               IdStrategy.CUSTOMIZE_NUMBER,
                                               name.id(), age.id(),
                                               city.id());
    Mockito.when(fakeObject.graph().mapPkId2Name(vl.properties()))
           .thenReturn(Arrays.asList(name.name(), age.name(), city.name()));

    String json = JsonUtil.toJson(vl);
    Assert.assertEquals("{\"id\":1,\"name\":\"person\"," +
                        "\"id_strategy\":\"CUSTOMIZE_NUMBER\"," +
                        "\"primary_keys\":[],\"nullable_keys\":[]," +
                        "\"index_labels\":[]," +
                        "\"properties\":[\"name\",\"age\",\"city\"]," +
                        "\"status\":\"CREATED\"," +
                        "\"ttl\":0,\"enable_label_index\":true," +
                        "\"user_data\":{}}", json);
}
 
Example 6
Source File: HugeAuthenticator.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
public String toJson() {
    UserJson json = new UserJson();
    json.username = this.username();
    json.role = this.role();
    json.client = this.client();
    return JsonUtil.toJson(json);
}
 
Example 7
Source File: JsonGraph.java    From hugegraph-tools with Apache License 2.0 5 votes vote down vote up
public static JsonEdge from(Edge e) {
    JsonEdge edge = new JsonEdge();
    edge.id = e.id();
    edge.label = e.label();
    edge.source = e.sourceId();
    edge.target = e.targetId();
    edge.properties = JsonUtil.toJson(e.properties());
    return edge;
}
 
Example 8
Source File: JsonUtilTest.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
@Test
public void testSerializeEdgeId() {
    Id id = new EdgeId(IdGenerator.of("1:marko"), Directions.OUT,
                       IdGenerator.of(1), "",
                       IdGenerator.of("1:josh"));
    String json = JsonUtil.toJson(id);
    Assert.assertEquals("\"S1:marko>1>>S1:josh\"", json);
}
 
Example 9
Source File: HugeTask.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
@Override
protected void set(V v) {
    String result = JsonUtil.toJson(v);
    checkPropertySize(result, P.RESULT);
    if (this.status(TaskStatus.SUCCESS)) {
        this.result = result;
    }
    // Will call done() and may cause to save to store
    super.set(v);
}
 
Example 10
Source File: MetricsAPI.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
@GET
@Timed
@Path("meters")
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed("admin")
public String meters() {
    ServerReporter reporter = ServerReporter.instance();
    return JsonUtil.toJson(reporter.meters());
}
 
Example 11
Source File: TextSerializer.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
private static String writeId(Id id) {
    if (id.number()) {
        return JsonUtil.toJson(id.asLong());
    } else {
        return JsonUtil.toJson(id.asString());
    }
}
 
Example 12
Source File: BinarySerializer.java    From hugegraph with Apache License 2.0 4 votes vote down vote up
private void writeUserdata(SchemaElement schema) {
    String userdataStr = JsonUtil.toJson(schema.userdata());
    writeString(HugeKeys.USER_DATA, userdataStr);
}
 
Example 13
Source File: HugeAuthenticator.java    From hugegraph with Apache License 2.0 4 votes vote down vote up
@Override
public String toString() {
    return JsonUtil.toJson(this);
}
 
Example 14
Source File: JsonSerializer.java    From hugegraph with Apache License 2.0 4 votes vote down vote up
@Override
public String writeVertexLabel(VertexLabel vertexLabel) {
    return JsonUtil.toJson(vertexLabel);
}
 
Example 15
Source File: TableSerializer.java    From hugegraph with Apache License 2.0 4 votes vote down vote up
protected Object writeProperty(PropertyKey propertyKey, Object value) {
    return JsonUtil.toJson(value);
}
 
Example 16
Source File: HugeResource.java    From hugegraph-client with Apache License 2.0 4 votes vote down vote up
@Override
public String toString() {
    return JsonUtil.toJson(this);
}
 
Example 17
Source File: RolePermission.java    From hugegraph with Apache License 2.0 4 votes vote down vote up
public String toJson() {
    return JsonUtil.toJson(this);
}
 
Example 18
Source File: JsonSerializer.java    From hugegraph with Apache License 2.0 4 votes vote down vote up
@Override
public String writeWeightedPath(NodeWithWeight path,
                                Iterator<Vertex> vertices) {
    return JsonUtil.toJson(ImmutableMap.of("path", path.toMap(),
                                           "vertices", vertices));
}
 
Example 19
Source File: JsonSerializer.java    From hugegraph with Apache License 2.0 4 votes vote down vote up
@Override
public String writeEdgeLabel(EdgeLabel edgeLabel) {
    return JsonUtil.toJson(edgeLabel);
}
 
Example 20
Source File: JsonUtilTest.java    From hugegraph with Apache License 2.0 4 votes vote down vote up
@Test
public void testSerializeLongId() {
    Id id = IdGenerator.of(123456L);
    String json = JsonUtil.toJson(id);
    Assert.assertEquals("123456", json);
}