Example usage for com.google.gson GsonBuilder create

List of usage examples for com.google.gson GsonBuilder create

Introduction

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

Prototype

public Gson create() 

Source Link

Document

Creates a Gson instance based on the current configuration.

Usage

From source file:com.aliakseipilko.flightdutytracker.presenter.impl.CreateFlightPresenter.java

License:Open Source License

public CreateFlightPresenter(CreateFlightFragment view) {
    this.view = view;
    repository = new FlightRepository();
    //This should be injected with Dagger, but its retarded
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.IDENTITY);
    codeConverter = new AirportCodeConverter(view.getContext().getApplicationContext(), gsonBuilder.create());
}

From source file:com.aliakseipilko.flightdutytracker.presenter.impl.EditFlightPresenter.java

License:Open Source License

public EditFlightPresenter(EditFlightFragment view) {
    this.view = view;
    repository = new FlightRepository();
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.IDENTITY);
    codeConverter = new AirportCodeConverter(view.getContext().getApplicationContext(), gsonBuilder.create());
}

From source file:com.alignace.chargeio.mapper.JsonMapper.java

License:Apache License

private static Gson getGson() {

    if (gson == null) {

        GsonBuilder builder = new GsonBuilder();
        builder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        builder.registerTypeAdapter(IPaymentMethod.class, new IPaymentTypeAdapter());
        builder.registerTypeAdapter(TokenReferenceOption.class, new TokenReferenceAdapter());
        gson = builder.create();
    }/*  www .  ja  v a 2 s .c o m*/

    return gson;
}

From source file:com.aliyun.fs.oss.utils.OSSClientAgent.java

License:Apache License

@SuppressWarnings("unchecked")
private ObjectMetadata getObjectMetadata(String bucket, String key, boolean retry)
        throws IOException, ServiceException, ClientException {
    try {//from  w w w .j  a  v  a 2  s.c  o m
        GsonBuilder builder = new GsonBuilder();
        builder.registerTypeAdapter(ObjectMetadata.class, new ObjectMetadataDeserializer());
        Gson gson = builder.create();

        Method method = this.ossClientClz.getMethod("getObjectMetadata", String.class, String.class);
        Object ret = method.invoke(this.ossClient, bucket, key);
        return gson.fromJson(gson.toJson(ret), ObjectMetadata.class);
    } catch (NoSuchMethodException e) {
        LOG.error(e.getMessage());
        return null;
    } catch (Exception e) {
        if (retry && updateOSSClient(e)) {
            return getObjectMetadata(bucket, key, false);
        } else {
            handleException(e);
            return null;
        }
    }
}

From source file:com.aliyun.fs.oss.utils.OSSClientAgent.java

License:Apache License

@SuppressWarnings("unchecked")
private OSSObject getObject(String bucket, String key, long start, long end, Configuration conf, boolean retry)
        throws IOException, ServiceException, ClientException {
    InputStream inputStream;/* w  w  w.  j ava  2 s . com*/
    try {
        Class GetObjectRequestClz = ResourceLoader.getInstance().getUrlClassLoader(conf)
                .loadClass("com.aliyun.oss.model.GetObjectRequest");
        Constructor cons0 = GetObjectRequestClz.getConstructor(String.class, String.class);
        Object getObjRequest = cons0.newInstance(bucket, key);
        Method method0 = GetObjectRequestClz.getMethod("setRange", Long.TYPE, Long.TYPE);
        method0.invoke(getObjRequest, start, end);

        Method method = this.ossClientClz.getMethod("getObject", GetObjectRequestClz);
        Object ret = method.invoke(this.ossClient, getObjRequest);

        Class OSSObjectClz = ResourceLoader.getInstance().getUrlClassLoader(conf)
                .loadClass("com.aliyun.oss.model.OSSObject");
        Method method1 = OSSObjectClz.getMethod("getObjectContent");
        inputStream = (InputStream) method1.invoke(ret);

        GsonBuilder builder = new GsonBuilder();
        builder.registerTypeAdapter(ObjectMetadata.class, new ObjectMetadataDeserializer());
        Gson gson = builder.create();
        Method method2 = OSSObjectClz.getMethod("getObjectMetadata");
        Object metadata = method2.invoke(ret);
        ObjectMetadata objectMetadata = gson.fromJson(gson.toJson(metadata), ObjectMetadata.class);

        OSSObject ossObject = new OSSObject();
        ossObject.setBucketName(bucket);
        ossObject.setKey(key);
        ossObject.setObjectContent(inputStream);
        ossObject.setObjectMetadata(objectMetadata);
        return ossObject;
    } catch (Exception e) {
        if (retry && updateOSSClient(e)) {
            return getObject(bucket, key, start, end, conf, false);
        } else {
            handleException(e);
            return null;
        }
    }
}

From source file:com.amazonaws.util.awsclientgenerator.generators.DirectFromC2jGenerator.java

License:Open Source License

public File generateSourceFromJson(String rawJson, String languageBinding, String serviceName)
        throws Exception {
    GsonBuilder gsonBuilder = new GsonBuilder();
    Gson gson = gsonBuilder.create();
    C2jServiceModel c2jServiceModel = gson.fromJson(rawJson, C2jServiceModel.class);

    return mainClientGenerator.generateSourceFromC2jModel(c2jServiceModel, serviceName, languageBinding);
}

From source file:com.amazonaws.util.awsclientgenerator.generators.MainClientGenerator.java

License:Open Source License

/**
 * Loads a json file into a service model object.
 *
 * @param path path to the json file.//  ww  w. jav a2 s  . co  m
 * @return Service Model (model of the json object in the specified file)
 * @throws IOException
 */
private C2jServiceModel loadServiceModelFromFile(final String path) throws IOException {

    StringBuilder inputJson = new StringBuilder();

    try (Reader reader = new InputStreamReader(getClass().getClassLoader().getResourceAsStream(path),
            "UTF-8")) {
        char[] inputBuffer = new char[1024];

        while (reader.read(inputBuffer) >= 0) {
            inputJson.append(new String(inputBuffer));
        }

        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson = gsonBuilder.create();
        return gson.fromJson(inputJson.toString(), C2jServiceModel.class);
    }
}

From source file:com.android.build.gradle.api.ApkOutput.java

License:Apache License

public static ImmutableList<SplitApkOutput> load(File inputFile) throws FileNotFoundException {

    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(SplitApkOutput.class, new SplitApkOutput.JsonDeserializer());
    Gson gson = gsonBuilder.create();

    ImmutableList.Builder<SplitApkOutput> builder = ImmutableList.builder();

    for (SplitApkOutput vo : gson.fromJson(new FileReader(inputFile), SplitApkOutput[].class)) {
        builder.add(vo);//from  ww w .  j  ava 2 s .  c  om
    }
    return builder.build();
}

From source file:com.android.build.gradle.internal.ExtraModelInfo.java

License:Apache License

public ExtraModelInfo(@NonNull Project project, boolean isLibrary) {
    super(computeModelQueryMode(project));
    this.project = project;
    this.isLibrary = isLibrary;
    errorFormatMode = computeErrorFormatMode(project);
    if (errorFormatMode == ErrorFormatMode.MACHINE_PARSABLE) {
        GsonBuilder gsonBuilder = new GsonBuilder();
        MessageJsonSerializer.registerTypeAdapters(gsonBuilder);
        mGson = gsonBuilder.create();
    } else {//from w ww  . j a v  a 2s  .  com
        mGson = null;
    }
}

From source file:com.android.build.gradle.internal.pipeline.SubStream.java

License:Apache License

public static Collection<SubStream> loadSubStreams(@NonNull File rootFolder) {
    final File jsonFile = new File(rootFolder, FN_FOLDER_CONTENT);
    if (!jsonFile.isFile()) {
        return ImmutableList.of();
    }/*w  w w .  j a v a2 s  .  com*/

    try (FileReader reader = new FileReader(jsonFile)) {

        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.registerTypeAdapter(SubStream.class, new SubStreamAdapter());
        Gson gson = gsonBuilder.create();

        Type recordType = new TypeToken<List<SubStream>>() {
        }.getType();
        return gson.fromJson(reader, recordType);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}