diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2015-09-16 09:55:20 +0200 |
---|---|---|
committer | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2015-09-17 11:36:11 +0200 |
commit | 36d08a1ef549c18bfa1637795370160e1793762e (patch) | |
tree | e2a73003e3bfd98c0a3d6de7647bf8f2e765aeca /sonar-core/src | |
parent | 0eb9c541726104ae3775920497548250cb91bbb2 (diff) | |
download | sonarqube-36d08a1ef549c18bfa1637795370160e1793762e.tar.gz sonarqube-36d08a1ef549c18bfa1637795370160e1793762e.zip |
Improve javadoc of ProtobufJsonFormat
Diffstat (limited to 'sonar-core/src')
-rw-r--r-- | sonar-core/src/main/java/org/sonar/core/util/ProtobufJsonFormat.java | 96 |
1 files changed, 93 insertions, 3 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/util/ProtobufJsonFormat.java b/sonar-core/src/main/java/org/sonar/core/util/ProtobufJsonFormat.java index c99e257e12a..04fac1c3979 100644 --- a/sonar-core/src/main/java/org/sonar/core/util/ProtobufJsonFormat.java +++ b/sonar-core/src/main/java/org/sonar/core/util/ProtobufJsonFormat.java @@ -32,8 +32,98 @@ import org.sonar.api.utils.text.JsonWriter; * Converts a Protocol Buffers message to JSON. Unknown fields, binary fields and groups * are not supported. Absent fields are ignored, so it's possible to distinguish * null strings (field is absent) and empty strings (field is present with value {@code ""}). - * <p/> - * TODO + * + * <h3>Example</h3> + * <pre> + * // protobuf specification + * message Foo { + * string name = 1; + * int32 count = 2; + * repeated string colors = 3; + * } + * + * // generated JSON + * { + * "name": "hello", + * "count": 32, + * "colors": ["blue", "red"] + * } + * </pre> + * + * <h3>Absent versus empty arrays</h3> + * <p> + * Protobuf does not support absent repeated field. The default value is empty. A pattern + * is defined by {@link ProtobufJsonFormat} to support the difference between absent and + * empty arrays when generating JSON. An intermediary message wrapping the array must be defined. + * It is automatically inlined and does not appear in the generated JSON. This wrapper message must: + * </p> + * <ul> + * <li>contain a single repeated field</li> + * <li>has the same name (case-insensitive) as the field</li> + * </ul> + * + * + * <p>Example:</p> + * <pre> + * // protobuf specification + * message Continent { + * string name = 1; + * Countries countries = 2; + * } + * + * // the message name ("Countries") is the same as the field 1 ("countries") + * message Countries { + * repeated string countries = 1; + * } + * + * // example of generated JSON if field 2 is not present + * { + * "name": "Europe", + * } + * + * // example of generated JSON if field 2 is present but inner array is empty. + * // The intermediary "countries" message is inline. + * { + * "name": "Europe", + * "countries": [] + * } + * + * // example of generated JSON if field 2 is present and inner array is not empty + * // The intermediary "countries" message is inline. + * { + * "name": "Europe", + * "countries": ["Spain", "Denmark"] + * } + * </pre> + * + * <h3>Array or map in map values</h3> + * <p> + * Map fields cannot be repeated. Values are scalar types or messages, but not arrays nor maps. In order + * to support multimaps (maps of lists) and maps of maps in JSON, the same pattern as for absent arrays + * can be used: + * </p> + * <p>Example:</p> + * <pre> + * // protobuf specification + * message Continent { + * string name = 1; + * map<string,Countries> countries_by_currency = 2; + * } + * + * // the message name ("Countries") is the same as the field 1 ("countries") + * message Countries { + * repeated string countries = 1; + * } + * + * // example of generated JSON. The intermediary "countries" message is inline. + * { + * "name": "Europe", + * "countries_by_currency": { + * "eur": ["Spain", "France"], + * "dkk": ["Denmark"] + * } + * } + * </pre> */ public class ProtobufJsonFormat { @@ -82,7 +172,7 @@ public class ProtobufJsonFormat { if (fieldDescriptor.isRepeated()) { writer.name(fieldDescriptor.getName()); if (fieldDescriptor.isMapField()) { - writeMap((Collection<MapEntry>)message.getField(fieldDescriptor), writer); + writeMap((Collection<MapEntry>) message.getField(fieldDescriptor), writer); } else { writeArray(writer, fieldDescriptor, (Collection) message.getField(fieldDescriptor)); } |