|
58 | 58 | * <pre> |
59 | 59 | * Tag value: 0 |
60 | 60 | * Value: |
61 | | - * 4-byte (unpacked int) – size of the entire array in bytes |
62 | | - * 4-byte (unpacked int) – number of elements in the array |
| 61 | + * 4-byte (unpacked int) - size of the entire array in bytes |
| 62 | + * 4-byte (unpacked int) - number of elements in the array |
63 | 63 | * The array fields, which are NSON values. Because NSON is schemaless the |
64 | 64 | * array elements may be of any type |
65 | 65 | * </pre> |
|
101 | 101 | * <pre> |
102 | 102 | * Tag value: 6 |
103 | 103 | * Value: |
104 | | - * 4-byte (unpacked int) – size of the entire map in bytes (value as written |
| 104 | + * 4-byte (unpacked int) - size of the entire map in bytes (value as written |
105 | 105 | * by Java's DataOutput.writeInt) |
106 | | - * 4-byte (unpacked int) – number of elements in the map (value as written |
| 106 | + * 4-byte (unpacked int) - number of elements in the map (value as written |
107 | 107 | * by Java's DataOutput.writeInt) |
108 | 108 | * The map fields, which are: |
109 | | - * key – String (see String below), cannot be null |
110 | | - * value – an NSON value. Because NSON is schemaless the map values may be |
| 109 | + * key - String (see String below), cannot be null |
| 110 | + * value - an NSON value. Because NSON is schemaless the map values may be |
111 | 111 | * of any type |
112 | 112 | * </pre> |
113 | 113 | * </li> |
|
147 | 147 | * </li> |
148 | 148 | * <li> Packed formats (TBD) |
149 | 149 | * <pre> |
150 | | - * packed int, long -- – describe algorithm |
151 | | - * unpacked int – endian? |
| 150 | + * packed int, long -- - describe algorithm |
| 151 | + * unpacked int - endian? |
152 | 152 | * Java BigDecimal/number format |
153 | 153 | * </pre> |
154 | 154 | * </li> |
@@ -990,6 +990,170 @@ public static MapValue readNsonMap(ByteInputStream in) |
990 | 990 | return (MapValue) readFieldValue(in); |
991 | 991 | } |
992 | 992 |
|
| 993 | + /** |
| 994 | + * Write a key/value integer map field to an Nson serializer. |
| 995 | + * @param ns the serializer to use |
| 996 | + * @param fieldName the name of the key |
| 997 | + * @param value the integer value to write |
| 998 | + * @throws IOException if there are problems writing to the serializer |
| 999 | + */ |
| 1000 | + public static void writeMapField(NsonSerializer ns, |
| 1001 | + String fieldName, |
| 1002 | + int value) throws IOException { |
| 1003 | + ns.startMapField(fieldName); |
| 1004 | + ns.integerValue(value); |
| 1005 | + ns.endMapField(fieldName); |
| 1006 | + } |
| 1007 | + |
| 1008 | + /** |
| 1009 | + * Write a key/value long map field to an Nson serializer. |
| 1010 | + * @param ns the serializer to use |
| 1011 | + * @param fieldName the name of the key |
| 1012 | + * @param value the long value to write |
| 1013 | + * @throws IOException if there are problems writing to the serializer |
| 1014 | + */ |
| 1015 | + public static void writeMapField(NsonSerializer ns, |
| 1016 | + String fieldName, |
| 1017 | + long value) throws IOException { |
| 1018 | + ns.startMapField(fieldName); |
| 1019 | + ns.longValue(value); |
| 1020 | + ns.endMapField(fieldName); |
| 1021 | + } |
| 1022 | + |
| 1023 | + /** |
| 1024 | + * Write a key/value byte array map field to an Nson serializer. |
| 1025 | + * @param ns the serializer to use |
| 1026 | + * @param fieldName the name of the key |
| 1027 | + * @param value the byte array value to write |
| 1028 | + * @throws IOException if there are problems writing to the serializer |
| 1029 | + */ |
| 1030 | + public static void writeMapField(NsonSerializer ns, |
| 1031 | + String fieldName, |
| 1032 | + byte[] value) throws IOException { |
| 1033 | + ns.startMapField(fieldName); |
| 1034 | + ns.binaryValue(value); |
| 1035 | + ns.endMapField(fieldName); |
| 1036 | + } |
| 1037 | + |
| 1038 | + /** |
| 1039 | + * Write a key/value byte array map field to an Nson serializer, |
| 1040 | + * given a specific offset and length. |
| 1041 | + * @param ns the serializer to use |
| 1042 | + * @param fieldName the name of the key |
| 1043 | + * @param value the byte arrayinteger value to write |
| 1044 | + * @param offset the offset into the given byte array to start from |
| 1045 | + * @param length the number of bytes from the offset to write |
| 1046 | + * @throws IOException if there are problems writing to the serializer |
| 1047 | + */ |
| 1048 | + public static void writeMapField(NsonSerializer ns, |
| 1049 | + String fieldName, |
| 1050 | + byte[] value, |
| 1051 | + int offset, |
| 1052 | + int length) throws IOException { |
| 1053 | + ns.startMapField(fieldName); |
| 1054 | + ns.binaryValue(value, offset, length); |
| 1055 | + ns.endMapField(fieldName); |
| 1056 | + } |
| 1057 | + |
| 1058 | + /** |
| 1059 | + * Write a key/value string array map field to an Nson serializer. |
| 1060 | + * Note: a null or empty array will result in nothing being written. |
| 1061 | + * @param ns the serializer to use |
| 1062 | + * @param fieldName the name of the key |
| 1063 | + * @param value the string array value to write |
| 1064 | + * @throws IOException if there are problems writing to the serializer |
| 1065 | + */ |
| 1066 | + public static void writeMapField(NsonSerializer ns, |
| 1067 | + String fieldName, |
| 1068 | + String[] value) throws IOException { |
| 1069 | + if (value == null || value.length == 0) { |
| 1070 | + return; |
| 1071 | + } |
| 1072 | + ns.startMapField(fieldName); |
| 1073 | + ns.startArray(0); |
| 1074 | + for (String s : value) { |
| 1075 | + ns.stringValue(s); |
| 1076 | + ns.incrSize(1); |
| 1077 | + } |
| 1078 | + ns.endArray(0); |
| 1079 | + ns.endMapField(fieldName); |
| 1080 | + } |
| 1081 | + |
| 1082 | + /** |
| 1083 | + * Write a key/value integer array map field to an Nson serializer. |
| 1084 | + * Note: a null or empty array will result in nothing being written. |
| 1085 | + * @param ns the serializer to use |
| 1086 | + * @param fieldName the name of the key |
| 1087 | + * @param value the integer array value to write |
| 1088 | + * @throws IOException if there are problems writing to the serializer |
| 1089 | + */ |
| 1090 | + public static void writeMapField(NsonSerializer ns, |
| 1091 | + String fieldName, |
| 1092 | + int[] value) throws IOException { |
| 1093 | + if (value == null || value.length == 0) { |
| 1094 | + return; |
| 1095 | + } |
| 1096 | + ns.startMapField(fieldName); |
| 1097 | + ns.startArray(0); |
| 1098 | + for (int i : value) { |
| 1099 | + ns.integerValue(i); |
| 1100 | + ns.incrSize(1); |
| 1101 | + } |
| 1102 | + ns.endArray(0); |
| 1103 | + ns.endMapField(fieldName); |
| 1104 | + } |
| 1105 | + |
| 1106 | + /** |
| 1107 | + * Write a key/value string map field to an Nson serializer. |
| 1108 | + * Note: a null value will result in nothing being written. |
| 1109 | + * @param ns the serializer to use |
| 1110 | + * @param fieldName the name of the key |
| 1111 | + * @param value the string value to write |
| 1112 | + * @throws IOException if there are problems writing to the serializer |
| 1113 | + */ |
| 1114 | + public static void writeMapField(NsonSerializer ns, |
| 1115 | + String fieldName, |
| 1116 | + String value) throws IOException { |
| 1117 | + /* allow null to be a no-op */ |
| 1118 | + if (value == null) { |
| 1119 | + return; |
| 1120 | + } |
| 1121 | + ns.startMapField(fieldName); |
| 1122 | + ns.stringValue(value); |
| 1123 | + ns.endMapField(fieldName); |
| 1124 | + } |
| 1125 | + |
| 1126 | + /** |
| 1127 | + * Write a key/value boolean map field to an Nson serializer. |
| 1128 | + * @param ns the serializer to use |
| 1129 | + * @param fieldName the name of the key |
| 1130 | + * @param value the boolean value to write |
| 1131 | + * @throws IOException if there are problems writing to the serializer |
| 1132 | + */ |
| 1133 | + public static void writeMapField(NsonSerializer ns, |
| 1134 | + String fieldName, |
| 1135 | + boolean value) throws IOException { |
| 1136 | + ns.startMapField(fieldName); |
| 1137 | + ns.booleanValue(value); |
| 1138 | + ns.endMapField(fieldName); |
| 1139 | + } |
| 1140 | + |
| 1141 | + /** |
| 1142 | + * Write a key/value MapValue map field to an Nson serializer. |
| 1143 | + * This will write a new map object with the given name. |
| 1144 | + * @param ns the serializer to use |
| 1145 | + * @param fieldName the name of the key |
| 1146 | + * @param value the MapValue value to write |
| 1147 | + * @throws IOException if there are problems writing to the serializer |
| 1148 | + */ |
| 1149 | + public static void writeMapField(NsonSerializer ns, |
| 1150 | + String fieldName, |
| 1151 | + MapValue value) throws IOException { |
| 1152 | + ns.startMapField(fieldName); |
| 1153 | + FieldValueEventHandler.generate(value, ns); |
| 1154 | + ns.endMapField(fieldName); |
| 1155 | + } |
| 1156 | + |
993 | 1157 | private static void readType(ByteInputStream in, int expected) |
994 | 1158 | throws IOException { |
995 | 1159 |
|
|
0 commit comments