Example usage for com.google.gson JsonDeserializer JsonDeserializer

List of usage examples for com.google.gson JsonDeserializer JsonDeserializer

Introduction

In this page you can find the example usage for com.google.gson JsonDeserializer JsonDeserializer.

Prototype

JsonDeserializer

Source Link

Usage

From source file:tweets.UserRepo.java

public static User getUser(String Pseudo) throws URISyntaxException {

    URI fromUri = new URI("http://localhost:7474/db/data/cypher");
    String fileContent = null;/*from w ww .ja v a2s  .  c o m*/
    WebResource resource = Client.create().resource(fromUri);
    String cypher = "{\n" + "  \"query\" : \"MATCH (n:user) WHERE n.Pseudo='" + Pseudo
            + "' RETURN n.name, n.Email, n.LastName, id(n), n.Pseudo \",\n" + "  \"params\" : {\n" + "  }\n"
            + "}";

    // POST JSON to the relationships URI
    ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON)
            .entity(cypher).post(ClientResponse.class);

    //System.out.println(response.getEntity(String.class));
    fileContent = response.getEntity(String.class);

    response.close();

    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(User.class, new JsonDeserializer<User>() {

        @Override
        public User deserialize(JsonElement je, Type type, JsonDeserializationContext jdc)
                throws JsonParseException {
            User s = null;
            try {
                JsonElement fields = je.getAsJsonObject().get("data");
                //System.out.println(je.getAsJsonObject());
                JsonArray coordinatesArray = fields.getAsJsonArray();
                System.out.println(coordinatesArray.get(0));
                String[] parts = coordinatesArray.get(0).toString().replace("\"", "").replace("[", "")
                        .replace("]", "").split(",");

                s = new User(parts[0], parts[2], parts[1], new URI(BASE_URI + "node/" + parts[3]), parts[4]);

            } catch (URISyntaxException | IndexOutOfBoundsException ex) {
                Logger.getLogger(UserRepo.class.getName()).log(Level.SEVERE, null, ex);
            }

            return s;
        }
    });
    Gson gson = builder.create();
    User s = gson.fromJson(fileContent, User.class);
    return s;

}

From source file:tweets.UserRepo.java

public static Tweet[] GetTweets(User S) throws URISyntaxException {
    URI fromUri = new URI("http://localhost:7474/db/data/cypher");

    WebResource resource = Client.create().resource(fromUri);
    String cypher = "{\n" + "  \"query\" : \" MATCH (n:user { Pseudo:'" + S.getPseudo()
            + "' })--(t:tweet) RETURN t.Message, id(t) \",\n" + "  \"params\" : {\n" + "  }\n" + "}";
    ;//from  w w w. j  a  va  2 s.  c o m

    // POST JSON to the relationships URI
    ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON)
            .entity(cypher).post(ClientResponse.class);

    String fileContent = response.getEntity(String.class);
    // System.out.println(fileContent);
    response.close();
    GsonBuilder builder = new GsonBuilder();

    builder.registerTypeAdapter(Tweet[].class, new JsonDeserializer<Tweet[]>() {

        @Override
        public Tweet[] deserialize(JsonElement je, Type type, JsonDeserializationContext jdc)
                throws JsonParseException {

            List<Tweet> s = new ArrayList<>();
            // System.out.println(je.getAsJsonObject().get("data").getAsJsonArray().get(1));
            // JsonArray coordinatesArray = fields.getAsJsonArray();

            for (int i = 0; i < je.getAsJsonObject().get("data").getAsJsonArray().size(); i++) {

                String[] parts = je.getAsJsonObject().get("data").getAsJsonArray().get(i).toString()
                        .replace("\"", "").replace("[", "").replace("]", "").split(",");
                try {
                    s.add(new Tweet(parts[0], new URI("http://localhost:7474/db/data/node/" + parts[1])));

                } catch (URISyntaxException ex) {
                    Logger.getLogger(UserRepo.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            return s.toArray(new Tweet[s.size()]);
        }
    });

    Gson gson = builder.create();
    Tweet[] M = gson.fromJson(fileContent, Tweet[].class);
    return M;

}

From source file:tweets.UserRepo.java

public static User[] GetFollowing(User S) throws URISyntaxException {
    URI fromUri = new URI("http://localhost:7474/db/data/cypher");

    WebResource resource = Client.create().resource(fromUri);
    String cypher = "{\n" + "  \"query\" : \" MATCH (n:user{ Pseudo: '" + S.getPseudo()
            + "'})-[labels:Follows]->(m) RETURN m.name, m.LastName, m.Email, id(m), m.Pseudo  \",\n"
            + "  \"params\" : {\n" + "  }\n" + "}";
    ;/*  ww  w  .  j a  v  a  2  s  . c  o  m*/

    // POST JSON to the relationships URI
    ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON)
            .entity(cypher).post(ClientResponse.class);

    String fileContent = response.getEntity(String.class);
    // System.out.println(fileContent);
    response.close();
    GsonBuilder builder = new GsonBuilder();

    builder.registerTypeAdapter(User[].class, new JsonDeserializer<User[]>() {

        @Override
        public User[] deserialize(JsonElement je, Type type, JsonDeserializationContext jdc)
                throws JsonParseException {

            List<User> s = new ArrayList<>();
            // System.out.println(je.getAsJsonObject().get("data").getAsJsonArray().get(1));
            // JsonArray coordinatesArray = fields.getAsJsonArray();

            for (int i = 0; i < je.getAsJsonObject().get("data").getAsJsonArray().size(); i++) {

                String[] parts = je.getAsJsonObject().get("data").getAsJsonArray().get(i).toString()
                        .replace("\"", "").replace("[", "").replace("]", "").split(",");
                try {
                    s.add(new User(parts[0], parts[1], parts[2], new URI(BASE_URI + "node/" + parts[3]),
                            parts[4]));

                } catch (URISyntaxException ex) {
                    Logger.getLogger(UserRepo.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            return s.toArray(new User[s.size()]);
        }
    });
    Gson gson = builder.create();

    User[] R = gson.fromJson(fileContent, User[].class);
    return R;

}

From source file:tweets.UserRepo.java

public static User[] GetFollowed(User S) throws URISyntaxException {
    URI fromUri = new URI("http://localhost:7474/db/data/cypher");

    WebResource resource = Client.create().resource(fromUri);
    String cypher = "{\n" + "  \"query\" : \" MATCH (n:user{ Pseudo: '" + S.getPseudo()
            + "'})<-[labels:Follows]-(m) RETURN m.name, m.LastName, m.Email, id(m), m.Pseudo  \",\n"
            + "  \"params\" : {\n" + "  }\n" + "}";
    ;//from w  ww  .jav  a  2s.c o  m

    // POST JSON to the relationships URI
    ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON)
            .entity(cypher).post(ClientResponse.class);

    String fileContent = response.getEntity(String.class);
    // System.out.println(fileContent);
    response.close();
    GsonBuilder builder = new GsonBuilder();

    builder.registerTypeAdapter(User[].class, new JsonDeserializer<User[]>() {

        @Override
        public User[] deserialize(JsonElement je, Type type, JsonDeserializationContext jdc)
                throws JsonParseException {

            List<User> s = new ArrayList<>();
            // System.out.println(je.getAsJsonObject().get("data").getAsJsonArray().get(1));
            // JsonArray coordinatesArray = fields.getAsJsonArray();

            for (int i = 0; i < je.getAsJsonObject().get("data").getAsJsonArray().size(); i++) {

                String[] parts = je.getAsJsonObject().get("data").getAsJsonArray().get(i).toString()
                        .replace("\"", "").replace("[", "").replace("]", "").split(",");
                try {
                    s.add(new User(parts[0], parts[1], parts[2], new URI(BASE_URI + "node/" + parts[3]),
                            parts[4]));

                } catch (URISyntaxException ex) {
                    Logger.getLogger(UserRepo.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            return s.toArray(new User[s.size()]);
        }
    });
    Gson gson = builder.create();

    User[] R = gson.fromJson(fileContent, User[].class);
    return R;

}