@Test public void duplicateFieldsAreTolerated() { String invalidJson = "{\"string\": \"tom\", \"string\": \"robert\"}"; byte[] result = SIMPLE_NAME_METADATA_DESCRIPTION.parseFromJson(invalidJson, false); assertThat(result).satisfiesAnyOf( (bytes) -> assertThat(bytes).containsExactly(PtBytes.toBytes("tom")), (bytes) -> assertThat(bytes).containsExactly(PtBytes.toBytes("robert"))); }
public byte[] parseFromJson(String json, boolean allowPrefix) { try { JsonNode jsonNode = OBJECT_MAPPER.readTree(json); Preconditions.checkState(jsonNode.isObject(), "Only JSON objects can be deserialized into parsed byte arrays. Passed json was: %s", json); ObjectNode objectNode = (ObjectNode) jsonNode; int numDefinedFields = countNumDefinedFields(objectNode); byte[][] bytes = new byte[numDefinedFields][]; Preconditions.checkArgument(numDefinedFields > 0, "JSON object needs a field named: %s. Passed json was: %s", rowParts.get(0).getComponentName(), json); Preconditions.checkArgument(allowPrefix || numDefinedFields == rowParts.size(), "JSON object has %s defined fields, but the number of row components is %s. Passed json was: %s", numDefinedFields, rowParts.size(), json); for (int i = 0; i < numDefinedFields; ++i) { NameComponentDescription desc = rowParts.get(i); JsonNode element = objectNode.get(desc.getComponentName()); String textRepresentation = element.isTextual() ? element.textValue() : String.valueOf(element); bytes[i] = desc.getType().convertFromString(textRepresentation); if (desc.isReverseOrder()) { EncodingUtils.flipAllBitsInPlace(bytes[i]); } } return Bytes.concat(bytes); } catch (JsonProcessingException e) { throw Throwables.throwUncheckedException(e); } }
NameMetadataDescription { public byte[] parseFromJson(String json, boolean allowPrefix) { try { JsonNode jsonNode = OBJECT_MAPPER.readTree(json); Preconditions.checkState(jsonNode.isObject(), "Only JSON objects can be deserialized into parsed byte arrays. Passed json was: %s", json); ObjectNode objectNode = (ObjectNode) jsonNode; int numDefinedFields = countNumDefinedFields(objectNode); byte[][] bytes = new byte[numDefinedFields][]; Preconditions.checkArgument(numDefinedFields > 0, "JSON object needs a field named: %s. Passed json was: %s", rowParts.get(0).getComponentName(), json); Preconditions.checkArgument(allowPrefix || numDefinedFields == rowParts.size(), "JSON object has %s defined fields, but the number of row components is %s. Passed json was: %s", numDefinedFields, rowParts.size(), json); for (int i = 0; i < numDefinedFields; ++i) { NameComponentDescription desc = rowParts.get(i); JsonNode element = objectNode.get(desc.getComponentName()); String textRepresentation = element.isTextual() ? element.textValue() : String.valueOf(element); bytes[i] = desc.getType().convertFromString(textRepresentation); if (desc.isReverseOrder()) { EncodingUtils.flipAllBitsInPlace(bytes[i]); } } return Bytes.concat(bytes); } catch (JsonProcessingException e) { throw Throwables.throwUncheckedException(e); } } }
NameMetadataDescription { public byte[] parseFromJson(String json, boolean allowPrefix) { try { JsonNode jsonNode = OBJECT_MAPPER.readTree(json); Preconditions.checkState(jsonNode.isObject(), "Only JSON objects can be deserialized into parsed byte arrays. Passed json was: %s", json); ObjectNode objectNode = (ObjectNode) jsonNode; int numDefinedFields = countNumDefinedFields(objectNode); byte[][] bytes = new byte[numDefinedFields][]; Preconditions.checkArgument(numDefinedFields > 0, "JSON object needs a field named: %s. Passed json was: %s", rowParts.get(0).getComponentName(), json); Preconditions.checkArgument(allowPrefix || numDefinedFields == rowParts.size(), "JSON object has %s defined fields, but the number of row components is %s. Passed json was: %s", numDefinedFields, rowParts.size(), json); for (int i = 0; i < numDefinedFields; ++i) { NameComponentDescription desc = rowParts.get(i); JsonNode element = objectNode.get(desc.getComponentName()); String textRepresentation = element.isTextual() ? element.textValue() : String.valueOf(element); bytes[i] = desc.getType().convertFromString(textRepresentation); if (desc.isReverseOrder()) { EncodingUtils.flipAllBitsInPlace(bytes[i]); } } return Bytes.concat(bytes); } catch (JsonProcessingException e) { throw Throwables.throwUncheckedException(e); } } NameMetadataDescription(); private  NameMetadataDescription(List<NameComponentDescription> components, int numberOfComponentsHashed); }
NameMetadataDescription { public byte[] parseFromJson(String json, boolean allowPrefix) { try { JsonNode jsonNode = OBJECT_MAPPER.readTree(json); Preconditions.checkState(jsonNode.isObject(), "Only JSON objects can be deserialized into parsed byte arrays. Passed json was: %s", json); ObjectNode objectNode = (ObjectNode) jsonNode; int numDefinedFields = countNumDefinedFields(objectNode); byte[][] bytes = new byte[numDefinedFields][]; Preconditions.checkArgument(numDefinedFields > 0, "JSON object needs a field named: %s. Passed json was: %s", rowParts.get(0).getComponentName(), json); Preconditions.checkArgument(allowPrefix || numDefinedFields == rowParts.size(), "JSON object has %s defined fields, but the number of row components is %s. Passed json was: %s", numDefinedFields, rowParts.size(), json); for (int i = 0; i < numDefinedFields; ++i) { NameComponentDescription desc = rowParts.get(i); JsonNode element = objectNode.get(desc.getComponentName()); String textRepresentation = element.isTextual() ? element.textValue() : String.valueOf(element); bytes[i] = desc.getType().convertFromString(textRepresentation); if (desc.isReverseOrder()) { EncodingUtils.flipAllBitsInPlace(bytes[i]); } } return Bytes.concat(bytes); } catch (JsonProcessingException e) { throw Throwables.throwUncheckedException(e); } } NameMetadataDescription(); private  NameMetadataDescription(List<NameComponentDescription> components, int numberOfComponentsHashed); static NameMetadataDescription create(List<NameComponentDescription> components); @Deprecated static NameMetadataDescription create(List<NameComponentDescription> components,
            boolean hasFirstComponentHash); static NameMetadataDescription create(List<NameComponentDescription> components, int numberOfComponentsHashed); static NameMetadataDescription create(String name, ValueType valueType); static NameMetadataDescription create(String name, ValueType valueType, ValueByteOrder byteOrder); static NameMetadataDescription safe(String name, ValueType valueType); List<NameComponentDescription> getRowParts(); int numberOfComponentsHashed(); List<RowNamePartitioner> getPartitionersForRow(); String renderToJson(byte[] name); byte[] parseFromJson(String json, boolean allowPrefix); TableMetadataPersistence.NameMetadataDescription.Builder persistToProto(); static NameMetadataDescription hydrateFromProto(TableMetadataPersistence.NameMetadataDescription message); @Override String toString(); @Override int hashCode(); @Override boolean equals(Object obj); }
NameMetadataDescription { public byte[] parseFromJson(String json, boolean allowPrefix) { try { JsonNode jsonNode = OBJECT_MAPPER.readTree(json); Preconditions.checkState(jsonNode.isObject(), "Only JSON objects can be deserialized into parsed byte arrays. Passed json was: %s", json); ObjectNode objectNode = (ObjectNode) jsonNode; int numDefinedFields = countNumDefinedFields(objectNode); byte[][] bytes = new byte[numDefinedFields][]; Preconditions.checkArgument(numDefinedFields > 0, "JSON object needs a field named: %s. Passed json was: %s", rowParts.get(0).getComponentName(), json); Preconditions.checkArgument(allowPrefix || numDefinedFields == rowParts.size(), "JSON object has %s defined fields, but the number of row components is %s. Passed json was: %s", numDefinedFields, rowParts.size(), json); for (int i = 0; i < numDefinedFields; ++i) { NameComponentDescription desc = rowParts.get(i); JsonNode element = objectNode.get(desc.getComponentName()); String textRepresentation = element.isTextual() ? element.textValue() : String.valueOf(element); bytes[i] = desc.getType().convertFromString(textRepresentation); if (desc.isReverseOrder()) { EncodingUtils.flipAllBitsInPlace(bytes[i]); } } return Bytes.concat(bytes); } catch (JsonProcessingException e) { throw Throwables.throwUncheckedException(e); } } NameMetadataDescription(); private  NameMetadataDescription(List<NameComponentDescription> components, int numberOfComponentsHashed); static NameMetadataDescription create(List<NameComponentDescription> components); @Deprecated static NameMetadataDescription create(List<NameComponentDescription> components,
            boolean hasFirstComponentHash); static NameMetadataDescription create(List<NameComponentDescription> components, int numberOfComponentsHashed); static NameMetadataDescription create(String name, ValueType valueType); static NameMetadataDescription create(String name, ValueType valueType, ValueByteOrder byteOrder); static NameMetadataDescription safe(String name, ValueType valueType); List<NameComponentDescription> getRowParts(); int numberOfComponentsHashed(); List<RowNamePartitioner> getPartitionersForRow(); String renderToJson(byte[] name); byte[] parseFromJson(String json, boolean allowPrefix); TableMetadataPersistence.NameMetadataDescription.Builder persistToProto(); static NameMetadataDescription hydrateFromProto(TableMetadataPersistence.NameMetadataDescription message); @Override String toString(); @Override int hashCode(); @Override boolean equals(Object obj); static final String HASH_ROW_COMPONENT_NAME; }