| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -18,7 +18,9 @@ | |||
| 18 | 18 | import com.fasterxml.jackson.core.Base64Variant; | |
| 19 | 19 | import com.fasterxml.jackson.core.JsonGenerationException; | |
| 20 | 20 | import com.fasterxml.jackson.core.ObjectCodec; | |
| 21 | + import com.fasterxml.jackson.core.SerializableString; | ||
| 21 | 22 | import com.fasterxml.jackson.core.base.GeneratorBase; | |
| 23 | + import com.fasterxml.jackson.core.io.SerializedString; | ||
| 22 | 24 | import com.fasterxml.jackson.core.json.JsonWriteContext; | |
| 23 | 25 | import org.msgpack.core.MessagePack; | |
| 24 | 26 | import org.msgpack.core.MessagePacker; | |
@@ -45,17 +47,17 @@ public class MessagePackGenerator | |||
| 45 | 47 | ||
| 46 | 48 | private abstract static class StackItem | |
| 47 | 49 | { | |
| 48 | - protected List<String> objectKeys = new ArrayList<String>(); | ||
| 50 | + protected List<Object> objectKeys = new ArrayList<Object>(); | ||
| 49 | 51 | protected List<Object> objectValues = new ArrayList<Object>(); | |
| 50 | 52 | ||
| 51 | - abstract void addKey(String key); | ||
| 53 | + abstract void addKey(Object key); | ||
| 52 | 54 | ||
| 53 | 55 | void addValue(Object value) | |
| 54 | 56 | { | |
| 55 | 57 | objectValues.add(value); | |
| 56 | 58 | } | |
| 57 | 59 | ||
| 58 | - abstract List<String> getKeys(); | ||
| 60 | + abstract List<Object> getKeys(); | ||
| 59 | 61 | ||
| 60 | 62 | List<Object> getValues() | |
| 61 | 63 | { | |
@@ -67,13 +69,13 @@ private static class StackItemForObject | |||
| 67 | 69 | extends StackItem | |
| 68 | 70 | { | |
| 69 | 71 | @Override | |
| 70 | - void addKey(String key) | ||
| 72 | + void addKey(Object key) | ||
| 71 | 73 | { | |
| 72 | 74 | objectKeys.add(key); | |
| 73 | 75 | } | |
| 74 | 76 | ||
| 75 | 77 | @Override | |
| 76 | - List<String> getKeys() | ||
| 78 | + List<Object> getKeys() | ||
| 77 | 79 | { | |
| 78 | 80 | return objectKeys; | |
| 79 | 81 | } | |
@@ -83,13 +85,13 @@ private static class StackItemForArray | |||
| 83 | 85 | extends StackItem | |
| 84 | 86 | { | |
| 85 | 87 | @Override | |
| 86 | - void addKey(String key) | ||
| 88 | + void addKey(Object key) | ||
| 87 | 89 | { | |
| 88 | 90 | throw new IllegalStateException("This method shouldn't be called"); | |
| 89 | 91 | } | |
| 90 | 92 | ||
| 91 | 93 | @Override | |
| 92 | - List<String> getKeys() | ||
| 94 | + List<Object> getKeys() | ||
| 93 | 95 | { | |
| 94 | 96 | throw new IllegalStateException("This method shouldn't be called"); | |
| 95 | 97 | } | |
@@ -164,7 +166,7 @@ public void writeEndObject() | |||
| 164 | 166 | popStackAndStoreTheItemAsValue(); | |
| 165 | 167 | } | |
| 166 | 168 | ||
| 167 | - private void packValue(Object v) | ||
| 169 | + private void pack(Object v) | ||
| 168 | 170 | throws IOException | |
| 169 | 171 | { | |
| 170 | 172 | MessagePacker messagePacker = getMessagePacker(); | |
@@ -256,16 +258,15 @@ private void packBigDecimal(BigDecimal decimal) | |||
| 256 | 258 | private void packObject(StackItemForObject stackItem) | |
| 257 | 259 | throws IOException | |
| 258 | 260 | { | |
| 259 | - List<String> keys = stackItem.getKeys(); | ||
| 261 | + List<Object> keys = stackItem.getKeys(); | ||
| 260 | 262 | List<Object> values = stackItem.getValues(); | |
| 261 | 263 | ||
| 262 | 264 | MessagePacker messagePacker = getMessagePacker(); | |
| 263 | 265 | messagePacker.packMapHeader(keys.size()); | |
| 264 | 266 | ||
| 265 | 267 | for (int i = 0; i < keys.size(); i++) { | |
| 266 | - messagePacker.packString(keys.get(i)); | ||
| 267 | - Object v = values.get(i); | ||
| 268 | - packValue(v); | ||
| 268 | + pack(keys.get(i)); | ||
| 269 | + pack(values.get(i)); | ||
| 269 | 270 | } | |
| 270 | 271 | } | |
| 271 | 272 | ||
@@ -279,7 +280,7 @@ private void packArray(StackItemForArray stackItem) | |||
| 279 | 280 | ||
| 280 | 281 | for (int i = 0; i < values.size(); i++) { | |
| 281 | 282 | Object v = values.get(i); | |
| 282 | - packValue(v); | ||
| 283 | + pack(v); | ||
| 283 | 284 | } | |
| 284 | 285 | } | |
| 285 | 286 | ||
@@ -290,6 +291,22 @@ public void writeFieldName(String name) | |||
| 290 | 291 | addKeyToStackTop(name); | |
| 291 | 292 | } | |
| 292 | 293 | ||
| 294 | + @Override | ||
| 295 | + public void writeFieldName(SerializableString name) | ||
| 296 | + throws IOException | ||
| 297 | + { | ||
| 298 | + if (name instanceof MessagePackSerializedString) { | ||
| 299 | + addKeyToStackTop(((MessagePackSerializedString) name).getRawValue()); | ||
| 300 | + } | ||
| 301 | + else if (name instanceof SerializedString) { | ||
| 302 | + addKeyToStackTop(name.getValue()); | ||
| 303 | + } | ||
| 304 | + else { | ||
| 305 | + System.out.println(name.getClass()); | ||
| 306 | + throw new IllegalArgumentException("Unsupported key: " + name); | ||
| 307 | + } | ||
| 308 | + } | ||
| 309 | + | ||
| 293 | 310 | @Override | |
| 294 | 311 | public void writeString(String text) | |
| 295 | 312 | throws IOException, JsonGenerationException | |
@@ -504,7 +521,7 @@ private StackItemForArray getStackTopForArray() | |||
| 504 | 521 | return (StackItemForArray) stackTop; | |
| 505 | 522 | } | |
| 506 | 523 | ||
| 507 | - private void addKeyToStackTop(String key) | ||
| 524 | + private void addKeyToStackTop(Object key) | ||
| 508 | 525 | { | |
| 509 | 526 | getStackTop().addKey(key); | |
| 510 | 527 | } | |
@@ -513,7 +530,7 @@ private void addValueToStackTop(Object value) | |||
| 513 | 530 | throws IOException | |
| 514 | 531 | { | |
| 515 | 532 | if (stack.isEmpty()) { | |
| 516 | - packValue(value); | ||
| 533 | + pack(value); | ||
| 517 | 534 | flushMessagePacker(); | |
| 518 | 535 | } | |
| 519 | 536 | else { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,36 @@ | |||
| 1 | + // | ||
| 2 | + // MessagePack for Java | ||
| 3 | + // | ||
| 4 | + // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + // you may not use this file except in compliance with the License. | ||
| 6 | + // You may obtain a copy of the License at | ||
| 7 | + // | ||
| 8 | + // http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + // | ||
| 10 | + // Unless required by applicable law or agreed to in writing, software | ||
| 11 | + // distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + // See the License for the specific language governing permissions and | ||
| 14 | + // limitations under the License. | ||
| 15 | + // | ||
| 16 | + package org.msgpack.jackson.dataformat; | ||
| 17 | + | ||
| 18 | + import com.fasterxml.jackson.core.JsonGenerationException; | ||
| 19 | + import com.fasterxml.jackson.core.JsonGenerator; | ||
| 20 | + import com.fasterxml.jackson.databind.SerializerProvider; | ||
| 21 | + import com.fasterxml.jackson.databind.ser.std.StdSerializer; | ||
| 22 | + | ||
| 23 | + import java.io.IOException; | ||
| 24 | + | ||
| 25 | + public class MessagePackKeySerializer | ||
| 26 | + extends StdSerializer<Object> { | ||
| 27 | + public MessagePackKeySerializer() { | ||
| 28 | + super(Object.class); | ||
| 29 | + } | ||
| 30 | + | ||
| 31 | + @Override | ||
| 32 | + public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) | ||
| 33 | + throws JsonGenerationException, IOException { | ||
| 34 | + jgen.writeFieldName(new MessagePackSerializedString(value)); | ||
| 35 | + } | ||
| 36 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,122 @@ | |||
| 1 | + // | ||
| 2 | + // MessagePack for Java | ||
| 3 | + // | ||
| 4 | + // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + // you may not use this file except in compliance with the License. | ||
| 6 | + // You may obtain a copy of the License at | ||
| 7 | + // | ||
| 8 | + // http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + // | ||
| 10 | + // Unless required by applicable law or agreed to in writing, software | ||
| 11 | + // distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + // See the License for the specific language governing permissions and | ||
| 14 | + // limitations under the License. | ||
| 15 | + // | ||
| 16 | + package org.msgpack.jackson.dataformat; | ||
| 17 | + | ||
| 18 | + import com.fasterxml.jackson.core.SerializableString; | ||
| 19 | + | ||
| 20 | + import java.io.IOException; | ||
| 21 | + import java.io.OutputStream; | ||
| 22 | + import java.nio.ByteBuffer; | ||
| 23 | + import java.nio.charset.Charset; | ||
| 24 | + | ||
| 25 | + public class MessagePackSerializedString | ||
| 26 | + implements SerializableString | ||
| 27 | + { | ||
| 28 | + private static final Charset UTF8 = Charset.forName("UTF-8"); | ||
| 29 | + private final Object value; | ||
| 30 | + | ||
| 31 | + public MessagePackSerializedString(Object value) | ||
| 32 | + { | ||
| 33 | + this.value = value; | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | + @Override | ||
| 37 | + public String getValue() | ||
| 38 | + { | ||
| 39 | + return value.toString(); | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | + @Override | ||
| 43 | + public int charLength() | ||
| 44 | + { | ||
| 45 | + return getValue().length(); | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + @Override | ||
| 49 | + public char[] asQuotedChars() | ||
| 50 | + { | ||
| 51 | + return getValue().toCharArray(); | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + @Override | ||
| 55 | + public byte[] asUnquotedUTF8() | ||
| 56 | + { | ||
| 57 | + return getValue().getBytes(UTF8); | ||
| 58 | + } | ||
| 59 | + | ||
| 60 | + @Override | ||
| 61 | + public byte[] asQuotedUTF8() | ||
| 62 | + { | ||
| 63 | + return ("\"" + getValue() + "\"").getBytes(UTF8); | ||
| 64 | + } | ||
| 65 | + | ||
| 66 | + @Override | ||
| 67 | + public int appendQuotedUTF8(byte[] bytes, int i) | ||
| 68 | + { | ||
| 69 | + return 0; | ||
| 70 | + } | ||
| 71 | + | ||
| 72 | + @Override | ||
| 73 | + public int appendQuoted(char[] chars, int i) | ||
| 74 | + { | ||
| 75 | + return 0; | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + @Override | ||
| 79 | + public int appendUnquotedUTF8(byte[] bytes, int i) | ||
| 80 | + { | ||
| 81 | + return 0; | ||
| 82 | + } | ||
| 83 | + | ||
| 84 | + @Override | ||
| 85 | + public int appendUnquoted(char[] chars, int i) | ||
| 86 | + { | ||
| 87 | + return 0; | ||
| 88 | + } | ||
| 89 | + | ||
| 90 | + @Override | ||
| 91 | + public int writeQuotedUTF8(OutputStream outputStream) | ||
| 92 | + throws IOException | ||
| 93 | + { | ||
| 94 | + return 0; | ||
| 95 | + } | ||
| 96 | + | ||
| 97 | + @Override | ||
| 98 | + public int writeUnquotedUTF8(OutputStream outputStream) | ||
| 99 | + throws IOException | ||
| 100 | + { | ||
| 101 | + return 0; | ||
| 102 | + } | ||
| 103 | + | ||
| 104 | + @Override | ||
| 105 | + public int putQuotedUTF8(ByteBuffer byteBuffer) | ||
| 106 | + throws IOException | ||
| 107 | + { | ||
| 108 | + return 0; | ||
| 109 | + } | ||
| 110 | + | ||
| 111 | + @Override | ||
| 112 | + public int putUnquotedUTF8(ByteBuffer byteBuffer) | ||
| 113 | + throws IOException | ||
| 114 | + { | ||
| 115 | + return 0; | ||
| 116 | + } | ||
| 117 | + | ||
| 118 | + public Object getRawValue() | ||
| 119 | + { | ||
| 120 | + return value; | ||
| 121 | + } | ||
| 122 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,42 @@ | |||
| 1 | + // | ||
| 2 | + // MessagePack for Java | ||
| 3 | + // | ||
| 4 | + // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + // you may not use this file except in compliance with the License. | ||
| 6 | + // You may obtain a copy of the License at | ||
| 7 | + // | ||
| 8 | + // http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + // | ||
| 10 | + // Unless required by applicable law or agreed to in writing, software | ||
| 11 | + // distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + // See the License for the specific language governing permissions and | ||
| 14 | + // limitations under the License. | ||
| 15 | + // | ||
| 16 | + package org.msgpack.jackson.dataformat; | ||
| 17 | + | ||
| 18 | + import com.fasterxml.jackson.databind.JavaType; | ||
| 19 | + import com.fasterxml.jackson.databind.JsonSerializer; | ||
| 20 | + import com.fasterxml.jackson.databind.SerializationConfig; | ||
| 21 | + import com.fasterxml.jackson.databind.cfg.SerializerFactoryConfig; | ||
| 22 | + import com.fasterxml.jackson.databind.ser.BeanSerializerFactory; | ||
| 23 | + | ||
| 24 | + public class MessagePackSerializerFactory | ||
| 25 | + extends BeanSerializerFactory | ||
| 26 | + { | ||
| 27 | + /** | ||
| 28 | + * Constructor for creating instances with specified configuration. | ||
| 29 | + * | ||
| 30 | + * @param config | ||
| 31 | + */ | ||
| 32 | + protected MessagePackSerializerFactory(SerializerFactoryConfig config) | ||
| 33 | + { | ||
| 34 | + super(config); | ||
| 35 | + } | ||
| 36 | + | ||
| 37 | + @Override | ||
| 38 | + public JsonSerializer<Object> createKeySerializer(SerializationConfig config, JavaType keyType, JsonSerializer<Object> defaultImpl) | ||
| 39 | + { | ||
| 40 | + return new MessagePackKeySerializer(); | ||
| 41 | + } | ||
| 42 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments