Search in sources :

Example 16 with GsonBuilder

use of com.google.gson.GsonBuilder in project immutables by immutables.

the class Ent method main.

public static void main(String... args) throws UnknownHostException {
    MongoClient client = new MongoClient("localhost");
    RepositorySetup setup = RepositorySetup.builder().database(client.getDB("test")).executor(MoreExecutors.listeningDecorator(Executors.newCachedThreadPool())).gson(new GsonBuilder().registerTypeAdapterFactory(new GsonAdaptersEnt()).create()).build();
    EntRepository repository = new EntRepository(setup);
    EntRepository.Criteria where = repository.criteria().uuid("8b7a881c-6ccb-4ada-8f6a-60cc99e6aa20").actionIn("BAN", "IPBAN");
    Criteria or = where.expiresAbsent().or().with(where).expiresGreaterThan(TimeInstant.of(1467364749679L));
    System.out.println(or);
    repository.find(or).fetchAll().getUnchecked();
}
Also used : Criteria(org.immutables.mongo.fixture.ent.EntRepository.Criteria) MongoClient(com.mongodb.MongoClient) GsonBuilder(com.google.gson.GsonBuilder) RepositorySetup(org.immutables.mongo.repository.RepositorySetup) Criteria(org.immutables.mongo.fixture.ent.EntRepository.Criteria)

Example 17 with GsonBuilder

use of com.google.gson.GsonBuilder in project scss-lint-plugin by idok.

the class Lint method parse.

//    public File file;
//
//    public static Lint read(String xml) {
//        XStream xstream = new XStream();
//        xstream.alias("lint", Lint.class);
//        xstream.alias("file", File.class);
//        xstream.alias("issue", Issue.class);
//        xstream.addImplicitCollection(File.class, "issues");
//        xstream.useAttributeFor(File.class, "name");
//        xstream.useAttributeFor(Issue.class, "linter");
//        xstream.useAttributeFor(Issue.class, "line");
//        xstream.useAttributeFor(Issue.class, "column");
//        xstream.useAttributeFor(Issue.class, "length");
//        xstream.useAttributeFor(Issue.class, "severity");
//        xstream.useAttributeFor(Issue.class, "reason");
//        return (Lint) xstream.fromXML(xml);
//    }
//
//    public static class File {
//        public String name;
//        public List<Issue> issues = new ArrayList<Issue>();
//    }
public static Map<String, List<Issue>> parse(String json) {
    GsonBuilder builder = new GsonBuilder();
    //        builder.registerTypeAdapterFactory(adapter);
    Gson g = builder.setPrettyPrinting().create();
    Type listType = new TypeToken<Map<String, List<Issue>>>() {
    }.getType();
    return g.fromJson(json, listType);
}
Also used : Type(java.lang.reflect.Type) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) Map(java.util.Map)

Example 18 with GsonBuilder

use of com.google.gson.GsonBuilder in project jphp by jphp-compiler.

the class JsonExtension method createGsonBuilderForDecode.

public static GsonBuilder createGsonBuilderForDecode(MemoryDeserializer memoryDeserializer) {
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(Memory.class, memoryDeserializer);
    return builder;
}
Also used : GsonBuilder(com.google.gson.GsonBuilder)

Example 19 with GsonBuilder

use of com.google.gson.GsonBuilder in project jphp by jphp-compiler.

the class JsonExtension method createGsonBuilder.

public static GsonBuilder createGsonBuilder(MemorySerializer memorySerializer) {
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(Memory.class, memorySerializer);
    builder.registerTypeAdapter(NullMemory.class, memorySerializer);
    builder.registerTypeAdapter(UndefinedMemory.class, memorySerializer);
    builder.registerTypeAdapter(ReferenceMemory.class, memorySerializer);
    builder.registerTypeAdapter(TrueMemory.class, memorySerializer);
    builder.registerTypeAdapter(FalseMemory.class, memorySerializer);
    builder.registerTypeAdapter(LongMemory.class, memorySerializer);
    builder.registerTypeAdapter(DoubleMemory.class, memorySerializer);
    builder.registerTypeAdapter(ObjectMemory.class, memorySerializer);
    builder.registerTypeAdapter(ArrayMemory.class, memorySerializer);
    builder.registerTypeAdapter(BinaryMemory.class, memorySerializer);
    builder.registerTypeAdapter(CharMemory.class, memorySerializer);
    builder.registerTypeAdapter(KeyValueMemory.class, memorySerializer);
    builder.registerTypeAdapter(StringBuilderMemory.class, memorySerializer);
    builder.registerTypeAdapter(StringMemory.class, memorySerializer);
    return builder;
}
Also used : GsonBuilder(com.google.gson.GsonBuilder)

Example 20 with GsonBuilder

use of com.google.gson.GsonBuilder in project jphp by jphp-compiler.

the class JsonFunctions method json_decode.

public static Memory json_decode(Environment env, String json, boolean assoc, int depth) {
    MemoryDeserializer memoryDeserializer = new MemoryDeserializer();
    memoryDeserializer.setEnv(env);
    GsonBuilder gsonBuilder = JsonExtension.createGsonBuilderForDecode(memoryDeserializer);
    memoryDeserializer.setAssoc(assoc);
    memoryDeserializer.setMaxDepth(depth);
    Gson gson = gsonBuilder.create();
    try {
        env.setUserValue(JsonFunctions.class.getName() + "#error", null);
        Memory r = gson.fromJson(json, Memory.class);
        if (r == null)
            return Memory.NULL;
        else
            return assoc ? r.toImmutable() : r;
    } catch (MemoryDeserializer.MaxDepthException e) {
        env.setUserValue(JsonFunctions.class.getName() + "#error", JsonConstants.JSON_ERROR_DEPTH);
    } catch (JsonSyntaxException e) {
        env.setUserValue(JsonFunctions.class.getName() + "#error", JsonConstants.JSON_ERROR_SYNTAX);
    } catch (JsonParseException e) {
        env.setUserValue(JsonFunctions.class.getName() + "#error", JsonConstants.JSON_ERROR_STATE_MISMATCH);
    }
    return Memory.NULL;
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) GsonBuilder(com.google.gson.GsonBuilder) Memory(php.runtime.Memory) StringMemory(php.runtime.memory.StringMemory) Gson(com.google.gson.Gson) JsonParseException(com.google.gson.JsonParseException) MemoryDeserializer(org.develnext.jphp.json.gson.MemoryDeserializer)

Aggregations

GsonBuilder (com.google.gson.GsonBuilder)222 Gson (com.google.gson.Gson)135 IOException (java.io.IOException)40 Type (java.lang.reflect.Type)24 ArrayList (java.util.ArrayList)24 Map (java.util.Map)24 JsonElement (com.google.gson.JsonElement)20 HashMap (java.util.HashMap)20 Test (org.junit.Test)18 JsonObject (com.google.gson.JsonObject)15 JsonSyntaxException (com.google.gson.JsonSyntaxException)13 Date (java.util.Date)13 List (java.util.List)13 Retrofit (retrofit2.Retrofit)13 InputStream (java.io.InputStream)11 OkHttpClient (okhttp3.OkHttpClient)11 File (java.io.File)9 InputStreamReader (java.io.InputStreamReader)9 JsonParseException (com.google.gson.JsonParseException)8 URL (java.net.URL)8