@Override public String toString() { try { return BatfishObjectMapper.mapper().writeValueAsString(this); } catch (JsonProcessingException e) { return "Couldn't serialize AutocompleteSuggestion: " + e.getMessage(); } } }
public String prettyPrint() { try { return BatfishObjectMapper.writePrettyString(this); } catch (JsonProcessingException e) { throw new BatfishException("Failed to pretty-print question", e); } }
@Override public void storeCompletionMetadata( CompletionMetadata completionMetadata, NetworkId networkId, SnapshotId snapshotId) throws IOException { Path completionMetadataPath = getSnapshotCompletionMetadataPath(networkId, snapshotId); mkdirs(completionMetadataPath.getParent()); FileUtils.write( completionMetadataPath.toFile(), BatfishObjectMapper.writeString(completionMetadata), UTF_8); }
@VisibleForTesting /* Helper method to add the specified collection to the serialized list at the specified path. */ static <T> void addToSerializedList( Path serializedObjectPath, @Nullable Collection<T> addition, TypeReference<List<T>> type) throws IOException { if (addition == null || addition.isEmpty()) { return; } List<T> baseList; if (serializedObjectPath.toFile().exists()) { baseList = BatfishObjectMapper.mapper().readValue(CommonUtil.readFile(serializedObjectPath), type); baseList.addAll(addition); } else { baseList = ImmutableList.copyOf(addition); } CommonUtil.writeFile(serializedObjectPath, BatfishObjectMapper.writePrettyString(baseList)); }
@Test public void testSerialization() throws IOException { String serialized = BatfishObjectMapper.writeString(IntegerSpace.PORTS); assertThat( BatfishObjectMapper.mapper().readValue(serialized, IntegerSpace.class), equalTo(IntegerSpace.PORTS)); }
private static Variable clone(Variable variable) throws IOException { return BatfishObjectMapper.clone(variable, Variable.class); }
private void savePermissions(Permissions p) { try { BatfishObjectMapper.prettyWriter().writeValue(_permsFile.toFile(), p); } catch (IOException e) { throw new BatfishException( String.format("Error saving permissions to '%s'", _permsFile.toAbsolutePath()), e); } }
@Test public void serializationCheck() throws IOException { String serialized = BatfishObjectMapper.writePrettyString(FAILURE_SPECIFIER); assertThat( BatfishObjectMapper.mapper().readValue(serialized, DispositionSpecifier.class), equalTo(FAILURE_SPECIFIER)); }
@Test public void testSerializationFields() throws JsonParseException, JsonMappingException, JsonProcessingException, IOException { Variable variable = new Variable(); String fieldName = "f1"; Field field = new Field(); field.setOptional(true); variable.setFields(ImmutableMap.of(fieldName, field)); // fields survive serialization cyle intact assertThat( BatfishObjectMapper.mapper() .readValue(BatfishObjectMapper.writeString(variable), Variable.class) .getFields() .get(fieldName) .getOptional(), equalTo(true)); }
private static InstanceData clone(InstanceData instanceData) throws IOException { return BatfishObjectMapper.clone(instanceData, InstanceData.class); }
public static void serializeAsJson(Path outputPath, Object object, String objectName) { try { BatfishObjectMapper.prettyWriter().writeValue(outputPath.toFile(), object); } catch (IOException e) { throw new BatfishException("Could not serialize " + objectName + " ", e); } }
private Users loadUsers() { try { return BatfishObjectMapper.mapper().readValue(_usersFile.toFile(), Users.class); } catch (IOException e) { throw new BatfishException( String.format("Error loading users from '%s'", _usersFile.toAbsolutePath()), e); } } }
public String prettyPrint() { try { return BatfishObjectMapper.writePrettyString(this); } catch (JsonProcessingException e) { throw new BatfishException("Failed to pretty print answer element", e); } }
@Test public void testSerialization() throws IOException { String serialized = BatfishObjectMapper.writePrettyString(RoutingProtocolSpecifier.ALL_PROTOCOLS_SPECIFIER); assertThat( BatfishObjectMapper.mapper().readValue(serialized, RoutingProtocolSpecifier.class), equalTo(RoutingProtocolSpecifier.ALL_PROTOCOLS_SPECIFIER)); }
private static Row toSearchFiltersRow( @Nullable AclLineMatchExpr description, Row row, boolean generateExplanations) { RowBuilder rowBuilder = Row.builder().putAll(row, row.getColumnNames()); if (generateExplanations) { /* * Sending the explanation to the client as a JSON blob. TODO: do something better. */ String jsonDescription; try { jsonDescription = BatfishObjectMapper.writeString(description); } catch (JsonProcessingException e) { e.printStackTrace(); jsonDescription = e.getMessage(); } rowBuilder.put(COL_HEADER_SPACE, jsonDescription); } return rowBuilder.build(); }
@Test public void testNullableSeveritySerialization() throws JsonParseException, JsonMappingException, JsonProcessingException, IOException { IssueConfigBean bean0 = new IssueConfigBean("major1", new MinorIssueConfig("minor1", 0, null)); IssueConfigBean beanNull = new IssueConfigBean("major1", new MinorIssueConfig("minor1", null, null)); IssueConfigBean bean0Deserialized = BatfishObjectMapper.mapper() .readValue(BatfishObjectMapper.writeString(bean0), IssueConfigBean.class); IssueConfigBean beanNullDeserialized = BatfishObjectMapper.mapper() .readValue(BatfishObjectMapper.writeString(beanNull), IssueConfigBean.class); new EqualsTester() .addEqualityGroup(bean0, bean0Deserialized) .addEqualityGroup(beanNull, beanNullDeserialized) .testEquals(); } }
@Test public void testJsonSerialization() throws IOException { DisjunctionChain dc = new DisjunctionChain(ImmutableList.of(BooleanExprs.TRUE)); assertThat(BatfishObjectMapper.clone(dc, DisjunctionChain.class), equalTo(dc)); }
public JSONObject getParsingResults(String networkName, String snapshotName) throws JsonProcessingException, JSONException { ParseVendorConfigurationAnswerElement pvcae = deserializeObject( getdirSnapshot(networkName, snapshotName) .resolve(Paths.get(BfConsts.RELPATH_OUTPUT, BfConsts.RELPATH_PARSE_ANSWER_PATH)), ParseVendorConfigurationAnswerElement.class); JSONObject warnings = new JSONObject(); SortedMap<String, Warnings> warningsMap = pvcae.getWarnings(); ObjectWriter writer = BatfishObjectMapper.prettyWriter(); for (String s : warningsMap.keySet()) { warnings.put(s, writer.writeValueAsString(warningsMap.get(s))); } return warnings; }
@Override public CompletionMetadata loadCompletionMetadata(NetworkId networkId, SnapshotId snapshotId) throws IOException { Path completionMetadataPath = getSnapshotCompletionMetadataPath(networkId, snapshotId); if (!Files.exists(completionMetadataPath)) { return CompletionMetadata.EMPTY; } String completionMetadataStr = FileUtils.readFileToString(completionMetadataPath.toFile(), UTF_8); return BatfishObjectMapper.mapper() .readValue(completionMetadataStr, new TypeReference<CompletionMetadata>() {}); }
public static synchronized void write(ReferenceLibrary data, Path dataPath) throws JsonProcessingException { CommonUtil.writeFile(dataPath, BatfishObjectMapper.writePrettyString(data)); } }