当前位置: 首页>>代码示例>>Java>>正文


Java GsonBuilder.create方法代码示例

本文整理汇总了Java中com.google.gson.GsonBuilder.create方法的典型用法代码示例。如果您正苦于以下问题:Java GsonBuilder.create方法的具体用法?Java GsonBuilder.create怎么用?Java GsonBuilder.create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.google.gson.GsonBuilder的用法示例。


在下文中一共展示了GsonBuilder.create方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: seedDatabaseOptions

import com.google.gson.GsonBuilder; //导入方法依赖的package包/类
@Override
public Observable<Boolean> seedDatabaseOptions() {

    GsonBuilder builder = new GsonBuilder().excludeFieldsWithoutExposeAnnotation();
    final Gson gson = builder.create();

    return mDbHelper.isOptionEmpty()
            .concatMap(new Function<Boolean, ObservableSource<? extends Boolean>>() {
                @Override
                public ObservableSource<? extends Boolean> apply(Boolean isEmpty)
                        throws Exception {
                    if (isEmpty) {
                        Type type = new TypeToken<List<Option>>() {
                        }
                                .getType();
                        List<Option> optionList = gson.fromJson(
                                CommonUtils.loadJSONFromAsset(mContext,
                                        AppConstants.SEED_DATABASE_OPTIONS),
                                type);

                        return saveOptionList(optionList);
                    }
                    return Observable.just(false);
                }
            });
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:27,代码来源:AppDataManager.java

示例2: testDeserializationWithMultipleTypes

import com.google.gson.GsonBuilder; //导入方法依赖的package包/类
@Test
public void testDeserializationWithMultipleTypes() {
    GsonBuilder gsonBuilder = new GsonBuilder();
    new GraphAdapterBuilder().addType(Company.class).addType(Employee.class)
            .registerOn(gsonBuilder);
    Gson gson = gsonBuilder.create();

    String json = "{'0x1':{'name':'Google','employees':['0x2','0x3']},"
            + "'0x2':{'name':'Jesse','company':'0x1'},"
            + "'0x3':{'name':'Joel','company':'0x1'}}";
    Company company = gson.fromJson(json, Company.class);
    assertEquals("Google", company.name);
    Employee jesse = company.employees.get(0);
    assertEquals("Jesse", jesse.name);
    assertEquals(company, jesse.company);
    Employee joel = company.employees.get(1);
    assertEquals("Joel", joel.name);
    assertEquals(company, joel.company);
}
 
开发者ID:sap-nocops,项目名称:Jerkoff,代码行数:20,代码来源:GraphAdapterBuilderTest.java

示例3: getEntityJSON

import com.google.gson.GsonBuilder; //导入方法依赖的package包/类
@Override
public String getEntityJSON() {
  Map<String, Object> values = new HashMap<String, Object>(1);
  values.put(JsonTagsZ.NAME, getName());
  values.put(JsonTagsZ.LATITUDE, getLatitude());
  values.put(JsonTagsZ.LONGITUDE, getLongitude());
  List<Map<String, Object>> userList = new ArrayList<Map<String, Object>>(1);
  for (IUserAccount userAccount : getUsers()) {
    Map<String, Object> userValues = new HashMap<String, Object>(1);
    userValues.put(JsonTagsZ.USER_ID, userAccount.getUserId());
    userValues.put(JsonTagsZ.USER, userAccount.getFullName());
    userValues.put(JsonTagsZ.MOBILE, userAccount.getMobilePhoneNumber());
    userList.add(userValues);
  }
  values.put(JsonTagsZ.USERS, userList);
  GsonBuilder builder = new GsonBuilder();
  Gson gson = builder.create();
  String jsonStr = gson.toJson(values);
  return jsonStr;
}
 
开发者ID:logistimo,项目名称:logistimo-web-service,代码行数:21,代码来源:Kiosk.java

示例4: deserializePacket

import com.google.gson.GsonBuilder; //导入方法依赖的package包/类
public static Packet deserializePacket(String jsonString, DeviceKeyChain deviceKeyChain) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Pack.class, new PackDeserializer());

    Gson gson = gsonBuilder.create();

    Packet packet = gson.fromJson(jsonString, Packet.class);

    if (packet.encryptedPack != null) {
        String key = getKey(deviceKeyChain, packet);
        String plainPack = Crypto.decrypt(packet.encryptedPack, key);
        packet.pack = gson.fromJson(plainPack, Pack.class);
    }

    return packet;
}
 
开发者ID:tomikaa87,项目名称:gree-remote,代码行数:17,代码来源:Utils.java

示例5: testRec

import com.google.gson.GsonBuilder; //导入方法依赖的package包/类
@Test
public void testRec() {
    try {
        GsonBuilder gsonBuilder = new GsonBuilder();
        // register custom adapter from configuration
        new GraphAdapterBuilder().addType(A.class).addType(B.class).registerOn(gsonBuilder);
        // gsonBuilder.registerTypeHierarchyAdapter(Object.class,
        // new ObjectSerializer(gsonBuilder.create()));
        // gsonBuilder.registerTypeHierarchyAdapter(Object.class,
        // new ObjectDeserializer(gsonBuilder.create()));
        Gson gson = gsonBuilder.create();

        A a = new A();
        B b = new B();
        a.setB(b);
        b.setA(a);

        String json = gson.toJson(a);
        LOG.info("json: " + json);
        A a2 = gson.fromJson(json, a.getClass());
        LOG.info("a: " + a2);

    } catch (Exception e) {
        LOG.error(e);
    }
}
 
开发者ID:sap-nocops,项目名称:Jerkoff,代码行数:27,代码来源:GsonTest.java

示例6: json2Object

import com.google.gson.GsonBuilder; //导入方法依赖的package包/类
/**
 * 将json转化为cls对象
 * @param jsonString json字符串
 * @param cls 对应的类
 * @return
 */
public static <T> T json2Object(String jsonString, Class<T> cls) {
    T t = null;
    try {
        GsonBuilder builder = new GsonBuilder();
        // 不转换没有 @Expose 注解的字段
        builder.excludeFieldsWithoutExposeAnnotation();
        Gson gson = builder.create();
        t = gson.fromJson(jsonString, cls);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return t;
}
 
开发者ID:Liuzhiyang94,项目名称:ComponentProjectDemo,代码行数:20,代码来源:JsonUtils.java

示例7: handleApiError

import com.google.gson.GsonBuilder; //导入方法依赖的package包/类
@Override
public void handleApiError(ANError error) {

    if (error == null || error.getErrorBody() == null) {
        getMvpView().onError(R.string.api_default_error);
        return;
    }

    if (error.getErrorCode() == Constants.API_STATUS_CODE_LOCAL_ERROR
            && error.getErrorDetail().equals(ANConstants.CONNECTION_ERROR)) {
        getMvpView().onError(R.string.connection_error);
        return;
    }

    if (error.getErrorCode() == Constants.API_STATUS_CODE_LOCAL_ERROR
            && error.getErrorDetail().equals(ANConstants.REQUEST_CANCELLED_ERROR)) {
        getMvpView().onError(R.string.api_retry_error);
        return;
    }

    final GsonBuilder builder = new GsonBuilder().excludeFieldsWithoutExposeAnnotation();
    final Gson gson = builder.create();

    try {
        ApiError apiError = gson.fromJson(error.getErrorBody(), ApiError.class);

        if (apiError == null || apiError.getMessage() == null) {
            getMvpView().onError(R.string.api_default_error);
            return;
        }

        switch (error.getErrorCode()) {
            case HttpsURLConnection.HTTP_UNAUTHORIZED:
            case HttpsURLConnection.HTTP_FORBIDDEN:
            case HttpsURLConnection.HTTP_INTERNAL_ERROR:
            case HttpsURLConnection.HTTP_NOT_FOUND:
            default:
                getMvpView().onError(apiError.getMessage());
        }
    } catch (JsonSyntaxException | NullPointerException e) {
        Log.e(TAG, "handleApiError", e);
        getMvpView().onError(R.string.api_default_error);
    }
}
 
开发者ID:ansh94,项目名称:DailyTech,代码行数:45,代码来源:BasePresenter.java

示例8: DataSerializer

import com.google.gson.GsonBuilder; //导入方法依赖的package包/类
public DataSerializer(Class<?> clazz) {
    this.clazz = clazz;

    GsonBuilder builder = new GsonBuilder();
    builder.setDateFormat(FORMAT_DATE);
    this.gson = builder.create();
}
 
开发者ID:servicecatalog,项目名称:oscm,代码行数:8,代码来源:DataSerializer.java

示例9: serialize

import com.google.gson.GsonBuilder; //导入方法依赖的package包/类
/**
 * {@inheritDoc}
 */
@Override
public String serialize() {
    final GsonBuilder builder = new GsonBuilder();
    builder.excludeFieldsWithoutExposeAnnotation();
    builder.disableHtmlEscaping();
    final Gson gson = builder.create();
    return gson.toJson(this);
}
 
开发者ID:NoraUi,项目名称:NoraUi,代码行数:12,代码来源:DemoModel.java

示例10: deserialize_point

import com.google.gson.GsonBuilder; //导入方法依赖的package包/类
@Test public void
deserialize_point(){
	GsonBuilder gsonBuilder = new GsonBuilder();
	gsonBuilder.registerTypeAdapter(PositionDto.class, new PositionDeserializer());
	gsonBuilder.registerTypeAdapter(PointDto.class, new PointDeserializer());
	Gson gson = gsonBuilder.create();
	String pointGeoJson = "{        \"type\": \"Point\",       \"coordinates\": [14.244158,   \n47.149861, 0.7890780]    }";
	
	PointDto pointDto = gson.fromJson(pointGeoJson, PointDto.class);
	assertThat(pointDto.getLongitude(), equalTo(14.244158));
	assertThat(pointDto.getLatitude(), equalTo(47.149861));
	assertThat(pointDto.getElevation(), equalTo(0.7890780));
}
 
开发者ID:mokszr,项目名称:ultimate-geojson,代码行数:14,代码来源:PointDeserializerShould.java

示例11: toJson

import com.google.gson.GsonBuilder; //导入方法依赖的package包/类
public static String toJson(Object object) {
    if (gson == null) {
        GsonBuilder builder = new GsonBuilder();
        builder.setDateFormat("yyyy-MM-dd HH:mm:ss");
        gson = builder.create();
    }
    String json = gson.toJson(object);
    return json;
}
 
开发者ID:abook23,项目名称:godlibrary,代码行数:10,代码来源:JsonUtils.java

示例12: provideGson

import com.google.gson.GsonBuilder; //导入方法依赖的package包/类
@Provides
@AppScope
Gson provideGson() {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
    return gsonBuilder.create();
}
 
开发者ID:Jusenr,项目名称:androidgithub,代码行数:8,代码来源:ApiModule.java

示例13: deserialize_linestring_empty_coordinates

import com.google.gson.GsonBuilder; //导入方法依赖的package包/类
@Test public void
deserialize_linestring_empty_coordinates(){
	GsonBuilder gsonBuilder = new GsonBuilder();
	gsonBuilder.registerTypeAdapter(PositionDto.class, new PositionDeserializer());
	gsonBuilder.registerTypeAdapter(MultiPointDto.class, new MultiPointDeserializer());
	Gson gson = gsonBuilder.create();
	String multiPointGeoJson = "{        \"type\": \"MultiPoint\",       \"coordinates\": [   ]   }";
	
	MultiPointDto multiPoint = gson.fromJson(multiPointGeoJson, MultiPointDto.class);
	assertThat(multiPoint.getPositions().size(), equalTo(0));
}
 
开发者ID:mokszr,项目名称:ultimate-geojson,代码行数:12,代码来源:MultiPointDeserializerShould.java

示例14: generateJson

import com.google.gson.GsonBuilder; //导入方法依赖的package包/类
private static String generateJson(GameDefinition gameDefinition) {
	GsonBuilder builder = new GsonBuilder();
	builder.setPrettyPrinting();
	Gson gson = builder.create();

	String result = gson.toJson(gameDefinition);
	return result;
}
 
开发者ID:hamilton-lima,项目名称:robolucha,代码行数:9,代码来源:DefaultGameDefinitionFileCreator.java

示例15: parseContent

import com.google.gson.GsonBuilder; //导入方法依赖的package包/类
private void parseContent(String content) {
    switch (this.type) {
        case Config.PROPERTIES:
            this.parseProperties(content);
            break;
        case Config.JSON:
            GsonBuilder builder = new GsonBuilder();
            Gson gson = builder.create();
            this.config = new ConfigSection(gson.fromJson(content, new TypeToken<LinkedHashMap<String, Object>>() {
            }.getType()));
            break;
        case Config.YAML:
            DumperOptions dumperOptions = new DumperOptions();
            dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
            Yaml yaml = new Yaml(dumperOptions);
            this.config = new ConfigSection(yaml.loadAs(content, LinkedHashMap.class));
            if (this.config == null) {
                this.config = new ConfigSection();
            }
            break;
        // case Config.SERIALIZED
        case Config.ENUM:
            this.parseList(content);
            break;
        default:
            this.correct = false;
    }
}
 
开发者ID:CoreXDevelopment,项目名称:CoreX,代码行数:29,代码来源:Config.java


注:本文中的com.google.gson.GsonBuilder.create方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。