@Test public void testInvalidIdRead() throws TabsJsonHelper.InvalidJsonException { final int blankTabId = Tab.Type.BLANK.getTabId(); final String emptyTabsJson = "{\"" + JSON_TABS_ARRAY_KEY + "\":[" + "{\"" + JSON_TAB_ID_KEY + "\":" + blankTabId + "}," + "{\"" + JSON_TAB_ID_KEY + "\":" + 12345678 + "}" + "]}"; final List<Tab> items = TabsJsonHelper.getTabsFromJson(emptyTabsJson); assertEquals("Should ignore the tab with invalid id", 1, items.size()); assertEquals(blankTabId, items.get(0).getTabId()); }
public static List<Tab> getTabsFromJson(@Nullable final String tabsJson) throws InvalidJsonException { if (tabsJson == null || tabsJson.isEmpty()) { return getDefaultTabs(); } final List<Tab> returnTabs = new ArrayList<>(); final JsonObject outerJsonObject; try { outerJsonObject = JsonParser.object().from(tabsJson); if (!outerJsonObject.has(JSON_TABS_ARRAY_KEY)) { throw new InvalidJsonException("JSON doesn't contain \"" + JSON_TABS_ARRAY_KEY + "\" array"); } final JsonArray tabsArray = outerJsonObject.getArray(JSON_TABS_ARRAY_KEY); for (final Object o : tabsArray) { if (!(o instanceof JsonObject)) { continue; } final Tab tab = Tab.from((JsonObject) o); if (tab != null) { returnTabs.add(tab); } } } catch (final JsonParserException e) { throw new InvalidJsonException(e); } if (returnTabs.isEmpty()) { return getDefaultTabs(); } return returnTabs; }
TabsJsonHelper { public static List<Tab> getTabsFromJson(@Nullable final String tabsJson) throws InvalidJsonException { if (tabsJson == null || tabsJson.isEmpty()) { return getDefaultTabs(); } final List<Tab> returnTabs = new ArrayList<>(); final JsonObject outerJsonObject; try { outerJsonObject = JsonParser.object().from(tabsJson); if (!outerJsonObject.has(JSON_TABS_ARRAY_KEY)) { throw new InvalidJsonException("JSON doesn't contain \"" + JSON_TABS_ARRAY_KEY + "\" array"); } final JsonArray tabsArray = outerJsonObject.getArray(JSON_TABS_ARRAY_KEY); for (final Object o : tabsArray) { if (!(o instanceof JsonObject)) { continue; } final Tab tab = Tab.from((JsonObject) o); if (tab != null) { returnTabs.add(tab); } } } catch (final JsonParserException e) { throw new InvalidJsonException(e); } if (returnTabs.isEmpty()) { return getDefaultTabs(); } return returnTabs; } }
TabsJsonHelper { public static List<Tab> getTabsFromJson(@Nullable final String tabsJson) throws InvalidJsonException { if (tabsJson == null || tabsJson.isEmpty()) { return getDefaultTabs(); } final List<Tab> returnTabs = new ArrayList<>(); final JsonObject outerJsonObject; try { outerJsonObject = JsonParser.object().from(tabsJson); if (!outerJsonObject.has(JSON_TABS_ARRAY_KEY)) { throw new InvalidJsonException("JSON doesn't contain \"" + JSON_TABS_ARRAY_KEY + "\" array"); } final JsonArray tabsArray = outerJsonObject.getArray(JSON_TABS_ARRAY_KEY); for (final Object o : tabsArray) { if (!(o instanceof JsonObject)) { continue; } final Tab tab = Tab.from((JsonObject) o); if (tab != null) { returnTabs.add(tab); } } } catch (final JsonParserException e) { throw new InvalidJsonException(e); } if (returnTabs.isEmpty()) { return getDefaultTabs(); } return returnTabs; } private  TabsJsonHelper(); }
TabsJsonHelper { public static List<Tab> getTabsFromJson(@Nullable final String tabsJson) throws InvalidJsonException { if (tabsJson == null || tabsJson.isEmpty()) { return getDefaultTabs(); } final List<Tab> returnTabs = new ArrayList<>(); final JsonObject outerJsonObject; try { outerJsonObject = JsonParser.object().from(tabsJson); if (!outerJsonObject.has(JSON_TABS_ARRAY_KEY)) { throw new InvalidJsonException("JSON doesn't contain \"" + JSON_TABS_ARRAY_KEY + "\" array"); } final JsonArray tabsArray = outerJsonObject.getArray(JSON_TABS_ARRAY_KEY); for (final Object o : tabsArray) { if (!(o instanceof JsonObject)) { continue; } final Tab tab = Tab.from((JsonObject) o); if (tab != null) { returnTabs.add(tab); } } } catch (final JsonParserException e) { throw new InvalidJsonException(e); } if (returnTabs.isEmpty()) { return getDefaultTabs(); } return returnTabs; } private  TabsJsonHelper(); static List<Tab> getTabsFromJson(@Nullable final String tabsJson); static String getJsonToSave(@Nullable final List<Tab> tabList); static List<Tab> getDefaultTabs(); }
TabsJsonHelper { public static List<Tab> getTabsFromJson(@Nullable final String tabsJson) throws InvalidJsonException { if (tabsJson == null || tabsJson.isEmpty()) { return getDefaultTabs(); } final List<Tab> returnTabs = new ArrayList<>(); final JsonObject outerJsonObject; try { outerJsonObject = JsonParser.object().from(tabsJson); if (!outerJsonObject.has(JSON_TABS_ARRAY_KEY)) { throw new InvalidJsonException("JSON doesn't contain \"" + JSON_TABS_ARRAY_KEY + "\" array"); } final JsonArray tabsArray = outerJsonObject.getArray(JSON_TABS_ARRAY_KEY); for (final Object o : tabsArray) { if (!(o instanceof JsonObject)) { continue; } final Tab tab = Tab.from((JsonObject) o); if (tab != null) { returnTabs.add(tab); } } } catch (final JsonParserException e) { throw new InvalidJsonException(e); } if (returnTabs.isEmpty()) { return getDefaultTabs(); } return returnTabs; } private  TabsJsonHelper(); static List<Tab> getTabsFromJson(@Nullable final String tabsJson); static String getJsonToSave(@Nullable final List<Tab> tabList); static List<Tab> getDefaultTabs();  }