當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。