Search in sources :

Example 71 with JsonParser

use of com.google.gson.JsonParser in project iosched by google.

the class DataExtractorTest method setUp.

/**
   * @throws java.lang.Exception
   */
@Before
public void setUp() throws Exception {
    fakeFetcher = new EntityFetcher() {

        @Override
        public JsonElement fetch(Enum<?> entityType, Map<String, String> params) throws IOException {
            String filename = "sample_" + entityType.name();
            if (params != null && params.get("page") != null && Integer.parseInt(params.get("page")) > 1) {
                filename += "_page" + params.get("page");
            }
            filename += ".json";
            InputStream stream = TestHelper.openTestDataFileStream(filename);
            JsonReader reader = new JsonReader(new InputStreamReader(stream, Charset.forName("UTF-8")));
            return new JsonParser().parse(reader);
        }
    };
    RemoteFilesEntityFetcherFactory.setBuilder(new FetcherBuilder() {

        @Override
        public FetcherBuilder setSourceFiles(String... filenames) {
            return this;
        }

        @Override
        public EntityFetcher build() {
            return fakeFetcher;
        }
    });
    sources = new ExtraInput().fetchAllDataSources();
    sources.putAll(new VendorDynamicInput(fakeFetcher).fetchAllDataSources());
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) IOException(java.io.IOException) FetcherBuilder(com.google.samples.apps.iosched.server.schedule.input.fetcher.RemoteFilesEntityFetcherFactory.FetcherBuilder) EntityFetcher(com.google.samples.apps.iosched.server.schedule.input.fetcher.EntityFetcher) JsonElement(com.google.gson.JsonElement) ExtraInput(com.google.samples.apps.iosched.server.schedule.server.input.ExtraInput) JsonReader(com.google.gson.stream.JsonReader) VendorDynamicInput(com.google.samples.apps.iosched.server.schedule.server.input.VendorDynamicInput) JsonParser(com.google.gson.JsonParser) Before(org.junit.Before)

Example 72 with JsonParser

use of com.google.gson.JsonParser in project iosched by google.

the class CloudFileManager method readFileAsJsonObject.

public JsonObject readFileAsJsonObject(GcsFilename file) throws IOException {
    GcsFileMetadata metadata = gcsService.getMetadata(file);
    if (metadata == null) {
        if (SystemProperty.environment.value() == SystemProperty.Environment.Value.Development) {
            // In the development server, try to fetch files on cloud storage via HTTP
            Logger.getAnonymousLogger().info("fetching " + file.getObjectName() + " at " + Config.CLOUD_STORAGE_BASE_URL + file.getObjectName());
            return RemoteJsonHelper.fetchJsonFromPublicURL(Config.CLOUD_STORAGE_BASE_URL + file.getObjectName());
        }
        return null;
    }
    GcsInputChannel readChannel = null;
    try {
        readChannel = gcsService.openReadChannel(file, 0);
        JsonElement element = new JsonParser().parse(Channels.newReader(readChannel, DEFAULT_CHARSET_NAME));
        return element.getAsJsonObject();
    } finally {
        if (readChannel != null) {
            readChannel.close();
        }
    }
}
Also used : JsonElement(com.google.gson.JsonElement) GcsInputChannel(com.google.appengine.tools.cloudstorage.GcsInputChannel) GcsFileMetadata(com.google.appengine.tools.cloudstorage.GcsFileMetadata) JsonParser(com.google.gson.JsonParser)

Example 73 with JsonParser

use of com.google.gson.JsonParser in project SimpleNews by liuling07.

the class NewsJsonUtils method readJsonNewsDetailBeans.

public static NewsDetailBean readJsonNewsDetailBeans(String res, String docId) {
    NewsDetailBean newsDetailBean = null;
    try {
        JsonParser parser = new JsonParser();
        JsonObject jsonObj = parser.parse(res).getAsJsonObject();
        JsonElement jsonElement = jsonObj.get(docId);
        if (jsonElement == null) {
            return null;
        }
        newsDetailBean = JsonUtils.deserialize(jsonElement.getAsJsonObject(), NewsDetailBean.class);
    } catch (Exception e) {
        LogUtils.e(TAG, "readJsonNewsBeans error", e);
    }
    return newsDetailBean;
}
Also used : JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) NewsDetailBean(com.lauren.simplenews.beans.NewsDetailBean) JsonParser(com.google.gson.JsonParser)

Example 74 with JsonParser

use of com.google.gson.JsonParser in project SimpleNews by liuling07.

the class WeatherJsonUtils method getCity.

/**
     * 从定位的json字串中获取城市
     * @param json
     * @return
     */
public static String getCity(String json) {
    JsonParser parser = new JsonParser();
    JsonObject jsonObj = parser.parse(json).getAsJsonObject();
    JsonElement status = jsonObj.get("status");
    if (status != null && "OK".equals(status.getAsString())) {
        JsonObject result = jsonObj.getAsJsonObject("result");
        if (result != null) {
            JsonObject addressComponent = result.getAsJsonObject("addressComponent");
            if (addressComponent != null) {
                JsonElement cityElement = addressComponent.get("city");
                if (cityElement != null) {
                    return cityElement.getAsString().replace("市", "");
                }
            }
        }
    }
    return null;
}
Also used : JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) JsonParser(com.google.gson.JsonParser)

Example 75 with JsonParser

use of com.google.gson.JsonParser in project SimpleNews by liuling07.

the class ImageJsonUtils method readJsonImageBeans.

/**
     * 将获取到的json转换为图片列表对象
     * @param res
     * @return
     */
public static List<ImageBean> readJsonImageBeans(String res) {
    List<ImageBean> beans = new ArrayList<ImageBean>();
    try {
        JsonParser parser = new JsonParser();
        JsonArray jsonArray = parser.parse(res).getAsJsonArray();
        for (int i = 1; i < jsonArray.size(); i++) {
            JsonObject jo = jsonArray.get(i).getAsJsonObject();
            ImageBean news = JsonUtils.deserialize(jo, ImageBean.class);
            beans.add(news);
        }
    } catch (Exception e) {
        LogUtils.e(TAG, "readJsonImageBeans error", e);
    }
    return beans;
}
Also used : JsonArray(com.google.gson.JsonArray) ArrayList(java.util.ArrayList) ImageBean(com.lauren.simplenews.beans.ImageBean) JsonObject(com.google.gson.JsonObject) JsonParser(com.google.gson.JsonParser)

Aggregations

JsonParser (com.google.gson.JsonParser)356 JsonObject (com.google.gson.JsonObject)285 JsonElement (com.google.gson.JsonElement)105 JsonArray (com.google.gson.JsonArray)57 IOException (java.io.IOException)43 Gson (com.google.gson.Gson)40 InputStreamReader (java.io.InputStreamReader)32 ArrayList (java.util.ArrayList)20 HashMap (java.util.HashMap)20 Map (java.util.Map)18 Type (java.lang.reflect.Type)15 JsonReader (com.google.gson.stream.JsonReader)12 Test (org.junit.Test)12 URL (java.net.URL)11 AssetManager (android.content.res.AssetManager)10 InputStream (java.io.InputStream)10 GsonBuilder (com.google.gson.GsonBuilder)9 UserType (com.glitchcog.fontificator.bot.UserType)8 EmojiType (com.glitchcog.fontificator.emoji.EmojiType)8 JsonParseException (com.google.gson.JsonParseException)8