本文整理匯總了Java中com.google.gson.FieldNamingPolicy類的典型用法代碼示例。如果您正苦於以下問題:Java FieldNamingPolicy類的具體用法?Java FieldNamingPolicy怎麽用?Java FieldNamingPolicy使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
FieldNamingPolicy類屬於com.google.gson包,在下文中一共展示了FieldNamingPolicy類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: provideRedditResource
import com.google.gson.FieldNamingPolicy; //導入依賴的package包/類
@Provides
public RedditResource provideRedditResource() {
Type redditPostListType = new TypeToken<List<RedditPost>>() {}.getType();
Gson gson = new GsonBuilder()
.registerTypeAdapter(redditPostListType, new RedditPostsDeserializer())
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://www.reddit.com/")
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
return retrofit.create(RedditResource.class);
}
示例2: httpsPost
import com.google.gson.FieldNamingPolicy; //導入依賴的package包/類
public T httpsPost(String jsonBody, Class<T> clazz) throws IOException {
URL url = new URL(HTTPS_PROTOCOL, ACCOUNT_KEY_SERVICE_HOST, HTTPS_PORT,
ACCOUNT_KEY_ENDPOINT);
httpsConnection = (HttpsURLConnection) url.openConnection();
httpsConnection.setRequestMethod(HttpRequestMethod.POST.toString());
setConnectionParameters(httpsConnection, HttpRequestMethod.POST);
httpsConnection.setFixedLengthStreamingMode(jsonBody.getBytes().length);
try (OutputStreamWriter out = new OutputStreamWriter(
httpsConnection.getOutputStream())) {
out.write(jsonBody);
}
StringBuilder sb = new StringBuilder();
try (BufferedReader in = new BufferedReader(new InputStreamReader(
httpsConnection.getInputStream()))) {
String inputLine;
while ((inputLine = in.readLine()) != null) {
sb.append(inputLine);
}
}
// setFieldNamingPolicy is used to here to convert from
// lower_case_with_underscore names retrieved from end point to camel
// case to match POJO class
Gson gson = new GsonBuilder().setFieldNamingPolicy(
FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
return gson.fromJson(sb.toString(), clazz);
}
示例3: testKfVideoMessageJson
import com.google.gson.FieldNamingPolicy; //導入依賴的package包/類
@Test
public void testKfVideoMessageJson(){
Video video = new Video();
video.setMediaId("MEDIA_ID");
video.setThumbMediaId("MEDIA_ID");
video.setTitle("TITLE");
video.setDescription("DESCRIPTION");
CustomVideoMessage videoMsg = new CustomVideoMessage();
videoMsg.setTouser("OPENID");
videoMsg.setVideo(video);
Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
//System.out.println(gson.toJson(videoMsg));
String str = gson.toJson(videoMsg);
Assert.assertNotNull(gson.fromJson(str, CustomVideoMessage.class));
}
示例4: testKfMusicMessageJson
import com.google.gson.FieldNamingPolicy; //導入依賴的package包/類
@Test
public void testKfMusicMessageJson(){
Music music = new Music();
music.setTitle("MUSIC_TITLE");
music.setDescription("MUSIC_DESCRIPTION");
music.setMusicurl("MUSIC_URL");
music.setHqmusicurl("HQ_MUSIC_URL");
music.setThumbMediaId("THUMB_MEDIA_ID");
CustomMusicMessage musicMsg = new CustomMusicMessage();
musicMsg.setTouser("OPENID");
musicMsg.setMusic(music);
Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
//System.out.println(gson.toJson(musicMsg));
String str = gson.toJson(musicMsg);
Assert.assertNotNull(gson.fromJson(str, CustomMusicMessage.class));
}
示例5: testKfNewsMessage
import com.google.gson.FieldNamingPolicy; //導入依賴的package包/類
@Test
public void testKfNewsMessage(){
List<Article> articles = new ArrayList<Article>();
Article a1 = new Article();
a1.setTitle("Happy Day");
a1.setDescription("Is Really A Happy Day");
a1.setUrl("URL");
a1.setPicurl("PIC_URL");
articles.add(a1);
Article a2 = new Article();
a2.setTitle("Happy Day");
a2.setDescription("Is Really A Happy Day");
a2.setUrl("URL");
a2.setPicurl("PIC_URL");
articles.add(a2);
CustomNewsMessage newsMsg = new CustomNewsMessage();
newsMsg.setTouser("OPENID");
newsMsg.setNews(new News(articles));
Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
//System.out.println(gson.toJson(newsMsg));
String str = gson.toJson(newsMsg);
Assert.assertNotNull(gson.fromJson(str, CustomNewsMessage.class));
}
示例6: testMapJson
import com.google.gson.FieldNamingPolicy; //導入依賴的package包/類
@Test
public void testMapJson(){
Map<String, Media> map = new HashMap<String, Media>();
Media media = new Media();
media.setMediaId("mediaId");
media.setThumbMediaId("thumbMediaId");
media.setType("news");
media.setCreatedAt(1892391);
map.put("template_id_short", media);
Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create();
//System.out.println(gson.toJson(newsMsg));
String jsonStr = gson.toJson(map);
gson.fromJson(jsonStr, HashMap.class);
}
示例7: getRetrofit
import com.google.gson.FieldNamingPolicy; //導入依賴的package包/類
public static Retrofit getRetrofit(@NonNull String blogUrl, @NonNull OkHttpClient httpClient) {
String baseUrl = NetworkUtils.makeAbsoluteUrl(blogUrl, "ghost/api/v0.1/");
Gson gson = new GsonBuilder()
.registerTypeAdapter(Date.class, new DateDeserializer())
.registerTypeAdapter(ConfigurationList.class, new ConfigurationListDeserializer())
.registerTypeAdapterFactory(new PostTypeAdapterFactory())
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.setExclusionStrategies(new RealmExclusionStrategy(), new AnnotationExclusionStrategy())
.create();
return new Retrofit.Builder()
.baseUrl(baseUrl)
.client(httpClient)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
// for HTML output (e.g., to get the client secret)
.addConverterFactory(StringConverterFactory.create())
// for raw JSONObject output (e.g., for the /configuration/about call)
.addConverterFactory(JSONObjectConverterFactory.create())
// for domain objects
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
}
示例8: getCourse
import com.google.gson.FieldNamingPolicy; //導入依賴的package包/類
@Nullable
public static Course getCourse(String zipFilePath) {
try {
final JBZipFile zipFile = new JBZipFile(zipFilePath);
final JBZipEntry entry = zipFile.getEntry(EduNames.COURSE_META_FILE);
if (entry == null) {
return null;
}
byte[] bytes = entry.getData();
final String jsonText = new String(bytes, CharsetToolkit.UTF8_CHARSET);
Gson gson = new GsonBuilder()
.registerTypeAdapter(Task.class, new StudySerializationUtils.Json.TaskAdapter())
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create();
return gson.fromJson(jsonText, Course.class);
}
catch (IOException e) {
LOG.error("Failed to unzip course archive");
LOG.error(e);
}
return null;
}
示例9: getTokens
import com.google.gson.FieldNamingPolicy; //導入依賴的package包/類
@Nullable
private static StepicWrappers.TokenInfo getTokens(@NotNull final List<NameValuePair> parameters) {
final Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
final HttpPost request = new HttpPost(EduStepicNames.TOKEN_URL);
request.setEntity(new UrlEncodedFormEntity(parameters, Consts.UTF_8));
try {
final CloseableHttpClient client = EduStepicClient.getHttpClient();
final CloseableHttpResponse response = client.execute(request);
final StatusLine statusLine = response.getStatusLine();
final HttpEntity responseEntity = response.getEntity();
final String responseString = responseEntity != null ? EntityUtils.toString(responseEntity) : "";
EntityUtils.consume(responseEntity);
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
return gson.fromJson(responseString, StepicWrappers.TokenInfo.class);
}
else {
LOG.warn("Failed to get tokens: " + statusLine.getStatusCode() + statusLine.getReasonPhrase());
}
}
catch (IOException e) {
LOG.warn(e.getMessage());
}
return null;
}
示例10: unpack
import com.google.gson.FieldNamingPolicy; //導入依賴的package包/類
public void unpack(@NonNull final JSONObject payload) {
if (DEBUG) {
MyLog.i(CLS_NAME, "unpacking");
}
final GsonBuilder builder = new GsonBuilder();
builder.disableHtmlEscaping();
builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
final Gson gson = builder.create();
nluNuance = gson.fromJson(payload.toString(), new TypeToken<NLUNuance>() {
}.getType());
new NLUCoerce(getNLUNuance(), getContext(), getSupportedLanguage(), getVRLocale(), getTTSLocale(),
getConfidenceArray(), getResultsArray()).coerce();
}
示例11: unpack
import com.google.gson.FieldNamingPolicy; //導入依賴的package包/類
public void unpack(@NonNull final JSONObject payload) {
if (DEBUG) {
MyLog.i(CLS_NAME, "unpacking");
}
final GsonBuilder builder = new GsonBuilder();
builder.disableHtmlEscaping();
builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
final Gson gson = builder.create();
nluSaiy = gson.fromJson(payload.toString(), new TypeToken<NLUSaiy>() {
}.getType());
new NLUCoerce(getNLUSaiy(), getContext(), getSupportedLanguage(), getVRLocale(), getTTSLocale(),
getConfidenceArray(), getResultsArray()).coerce();
}
示例12: create
import com.google.gson.FieldNamingPolicy; //導入依賴的package包/類
public Gson create() {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Id.class, new IdSerializer());
gsonBuilder.registerTypeAdapter(Type.class, new TypeSerializer());
gsonBuilder.registerTypeAdapter(Visitor.class, new VisitorSerializer());
gsonBuilder.registerTypeAdapter(Session.class, new SessionSerializer());
gsonBuilder.registerTypeAdapter(Map.class, new MapSerializer());
gsonBuilder.registerTypeAdapter(Payload.class, new MapSerializer());
gsonBuilder.serializeNulls();
gsonBuilder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
gsonBuilder.setLongSerializationPolicy(LongSerializationPolicy.STRING);
gsonBuilder.enableComplexMapKeySerialization();
return gsonBuilder.create();
}
示例13: build
import com.google.gson.FieldNamingPolicy; //導入依賴的package包/類
@VisibleForTesting
public StackApi build(String apiHost, Interceptor idlingInterceptor) {
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder()
.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY));
if (idlingInterceptor != null) {
clientBuilder.addInterceptor(idlingInterceptor);
}
return new Retrofit.Builder()
.baseUrl(apiHost)
.client(clientBuilder.build())
.addConverterFactory(GsonConverterFactory.create(
new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create()
))
.addCallAdapterFactory(RxJavaCallAdapterFactory.createWithScheduler(Schedulers.io()))
.build()
.create(StackApi.class);
}
示例14: deserialize
import com.google.gson.FieldNamingPolicy; //導入依賴的package包/類
@Override
public ArrayList<PDBrand> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject jsonObject = json.getAsJsonObject();
if (jsonObject.has("brands")) {
Gson gson = new GsonBuilder()
.registerTypeAdapter(PDBrand.class, new PDBrandDeserializer())
.registerTypeAdapter(long.class, new PDLongDeserializer())
.registerTypeAdapter(int.class, new PDIntDeserializer())
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create();
JsonArray brandsArray = jsonObject.getAsJsonArray("brands");
return gson.fromJson(brandsArray, typeOfT);
}
return new ArrayList<>();
}
示例15: deserialize
import com.google.gson.FieldNamingPolicy; //導入依賴的package包/類
@Override
public PDSocialMediaFriend deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create();
JsonObject jsonObject = json.getAsJsonObject();
PDSocialMediaFriend friend = gson.fromJson(jsonObject, PDSocialMediaFriend.class);
if (jsonObject.has("picture")) {
JsonObject pictureObject = jsonObject.getAsJsonObject("picture");
if (pictureObject.has("data")) {
JsonObject pictureDataObject = pictureObject.getAsJsonObject("data");
friend.setImageUrl(pictureDataObject.get("url").getAsString());
}
}
return friend;
}