@Test public void testInvalidRead() { final List<String> invalidList = Arrays.asList( "{\"notTabsArray\":[]}", "{invalidJSON]}", "{}" ); for (final String invalidContent : invalidList) { try { TabsJsonHelper.getTabsFromJson(invalidContent); fail("didn't throw exception"); } catch (final Exception e) { final boolean isExpectedException = e instanceof TabsJsonHelper.InvalidJsonException; assertTrue("\"" + e.getClass().getSimpleName() + "\" is not the expected exception", isExpectedException); } } }
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();  }