下面列出了com.fasterxml.jackson.core.JsonGenerator#writeBinaryField ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* for attribute valueByteArray serialization
*/
protected String serializeValueByteArray(byte[] value) {
if (value==null) {
return null;
}
KriptonJsonContext context=KriptonBinder.jsonBind();
try (KriptonByteArrayOutputStream stream=new KriptonByteArrayOutputStream(); JacksonWrapperSerializer wrapper=context.createSerializer(stream)) {
JsonGenerator jacksonSerializer=wrapper.jacksonGenerator;
jacksonSerializer.writeStartObject();
int fieldCount=0;
if (value!=null) {
fieldCount++;
jacksonSerializer.writeBinaryField("valueByteArray", value);
}
jacksonSerializer.writeEndObject();
jacksonSerializer.flush();
return stream.toString();
} catch(Exception e) {
e.printStackTrace();
throw(new KriptonRuntimeException(e.getMessage()));
}
}
/**
* Faster serialization for params of Actor's timer.
*
* @param timer Timer's to be serialized.
* @return JSON String.
* @throws IOException If cannot generate JSON.
*/
private byte[] serialize(ActorTimer<?> timer) throws IOException {
if (timer == null) {
return null;
}
try (ByteArrayOutputStream writer = new ByteArrayOutputStream()) {
JsonGenerator generator = JSON_FACTORY.createGenerator(writer);
generator.writeStartObject();
generator.writeStringField("dueTime", DurationUtils.convertDurationToDaprFormat(timer.getDueTime()));
generator.writeStringField("period", DurationUtils.convertDurationToDaprFormat(timer.getPeriod()));
generator.writeStringField("callback", timer.getCallback());
if (timer.getState() != null) {
generator.writeBinaryField("data", this.serialize(timer.getState()));
}
generator.writeEndObject();
generator.close();
writer.flush();
return writer.toByteArray();
}
}
@Override
public int serializeOnJacksonAsString(Person object, JsonGenerator jacksonSerializer) throws
Exception {
jacksonSerializer.writeStartObject();
int fieldCount=0;
// Serialized Field:
// field id (mapped with "id")
jacksonSerializer.writeStringField("id", PrimitiveUtils.writeLong(object.id));
// field image (mapped with "image")
if (object.image!=null) {
fieldCount++;
jacksonSerializer.writeBinaryField("image", object.image);
}
// field name (mapped with "name")
if (object.name!=null) {
fieldCount++;
jacksonSerializer.writeStringField("name", object.name);
}
jacksonSerializer.writeEndObject();
return fieldCount;
}
/**
* for attribute valueByteArray serialization
*/
protected String serializeValueByteArray(byte[] value) {
if (value==null) {
return null;
}
KriptonJsonContext context=KriptonBinder.jsonBind();
try (KriptonByteArrayOutputStream stream=new KriptonByteArrayOutputStream(); JacksonWrapperSerializer wrapper=context.createSerializer(stream)) {
JsonGenerator jacksonSerializer=wrapper.jacksonGenerator;
jacksonSerializer.writeStartObject();
int fieldCount=0;
if (value!=null) {
fieldCount++;
jacksonSerializer.writeBinaryField("valueByteArray", value);
}
jacksonSerializer.writeEndObject();
jacksonSerializer.flush();
return stream.toString();
} catch(Exception e) {
e.printStackTrace();
throw(new KriptonRuntimeException(e.getMessage()));
}
}
@Override
protected void doSerialize(EncryptionKey encryptionKey, JsonGenerator jgen, SerializerProvider provider)
throws IOException
{
jgen.writeBinaryField(KEY_FIELD, encryptionKey.getKey());
jgen.writeBinaryField(NONCE_FIELD, encryptionKey.getNonce());
}
@Override
public void serialize(Schema schema, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
throws IOException
{
jsonGenerator.writeStartObject();
ByteArrayOutputStream out = new ByteArrayOutputStream();
serDe.serialize(schema, out);
jsonGenerator.writeBinaryField(SCHEMA_FIELD_NAME, out.toByteArray());
jsonGenerator.writeEndObject();
}
public TransactionCompletion receiveFiles() throws IOException {
Transaction transaction = siteToSiteClient.createTransaction(TransferDirection.RECEIVE);
JsonGenerator jsonGenerator = new JsonFactory().createJsonGenerator(output);
jsonGenerator.writeStartArray();
DataPacket dataPacket;
while ((dataPacket = transaction.receive()) != null) {
jsonGenerator.writeStartObject();
jsonGenerator.writeFieldName("attributes");
jsonGenerator.writeStartObject();
Map<String, String> attributes = dataPacket.getAttributes();
if (attributes != null) {
for (Map.Entry<String, String> stringStringEntry : attributes.entrySet()) {
jsonGenerator.writeStringField(stringStringEntry.getKey(), stringStringEntry.getValue());
}
}
jsonGenerator.writeEndObject();
InputStream data = dataPacket.getData();
if (data != null) {
jsonGenerator.writeBinaryField("data", IOUtils.toByteArray(data));
}
jsonGenerator.writeEndObject();
}
jsonGenerator.writeEndArray();
jsonGenerator.close();
transaction.confirm();
return transaction.complete();
}
@Override
public int serializeOnJackson(Bean87A_2 object, JsonGenerator jacksonSerializer) throws
Exception {
jacksonSerializer.writeStartObject();
int fieldCount=0;
// Serialized Field:
// field attributeURL (mapped with "attributeURL")
if (object.attributeURL!=null) {
fieldCount++;
jacksonSerializer.writeBinaryField("attributeURL", TypeAdapterUtils.toData(UrlByteArrayTypeAdapter.class, object.attributeURL));
}
// field dataURL (mapped with "dataURL")
if (object.dataURL!=null) {
fieldCount++;
jacksonSerializer.writeBinaryField("dataURL", TypeAdapterUtils.toData(UrlByteArrayTypeAdapter.class, object.dataURL));
}
// field elementURL (mapped with "elementURL")
if (object.elementURL!=null) {
fieldCount++;
jacksonSerializer.writeBinaryField("elementURL", TypeAdapterUtils.toData(UrlByteArrayTypeAdapter.class, object.elementURL));
}
jacksonSerializer.writeEndObject();
return fieldCount;
}
/**
* Wraps data in the "data" attribute in a JSON object.
*
* @param data bytes to be wrapped into the "data" attribute in a JSON object.
* @return String to be sent to Dapr's API.
* @throws IOException If there's is any issue reading the data or wraping it
*/
public byte[] wrapData(final byte[] data) throws IOException {
try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
JsonGenerator generator = JSON_FACTORY.createGenerator(output);
generator.writeStartObject();
if (data != null) {
generator.writeBinaryField("data", data);
}
generator.writeEndObject();
generator.close();
output.flush();
return output.toByteArray();
}
}
@Override
public int serializeOnJacksonAsString(Bean02 object, JsonGenerator jacksonSerializer) throws
Exception {
jacksonSerializer.writeStartObject();
int fieldCount=0;
// Serialized Field:
// field content (mapped with "content")
if (object.getContent()!=null) {
fieldCount++;
jacksonSerializer.writeBinaryField("content", object.getContent());
}
// field id (mapped with "id")
jacksonSerializer.writeStringField("id", PrimitiveUtils.writeLong(object.getId()));
// field text (mapped with "text")
if (object.getText()!=null) {
fieldCount++;
jacksonSerializer.writeStringField("text", object.getText());
}
// field value (mapped with "value")
jacksonSerializer.writeStringField("value", PrimitiveUtils.writeLong(object.getValue()));
jacksonSerializer.writeEndObject();
return fieldCount;
}
/**
* Saves state changes transactionally.
* [
* {
* "operation": "upsert",
* "request": {
* "key": "key1",
* "value": "myData"
* }
* },
* {
* "operation": "delete",
* "request": {
* "key": "key2"
* }
* }
* ]
*
* @param actorType Name of the actor being changed.
* @param actorId Identifier of the actor being changed.
* @param stateChanges Collection of changes to be performed transactionally.
* @return Void.
*/
Mono<Void> apply(String actorType, ActorId actorId, ActorStateChange... stateChanges) {
if ((stateChanges == null) || stateChanges.length == 0) {
return Mono.empty();
}
int count = 0;
// Constructing the JSON via a stream API to avoid creating transient objects to be instantiated.
byte[] payload = null;
try (ByteArrayOutputStream writer = new ByteArrayOutputStream()) {
JsonGenerator generator = JSON_FACTORY.createGenerator(writer);
// Start array
generator.writeStartArray();
for (ActorStateChange stateChange : stateChanges) {
if ((stateChange == null) || (stateChange.getChangeKind() == null)) {
continue;
}
String operationName = stateChange.getChangeKind().getDaprStateChangeOperation();
if ((operationName == null) || (operationName.length() == 0)) {
continue;
}
count++;
// Start operation object.
generator.writeStartObject();
generator.writeStringField("operation", operationName);
// Start request object.
generator.writeObjectFieldStart("request");
generator.writeStringField("key", stateChange.getStateName());
if ((stateChange.getChangeKind() == ActorStateChangeKind.UPDATE)
|| (stateChange.getChangeKind() == ActorStateChangeKind.ADD)) {
byte[] data = this.stateSerializer.serialize(stateChange.getValue());
if (data != null) {
if (this.isStateSerializerDefault) {
// DefaultObjectSerializer is a JSON serializer, so we just pass it on.
generator.writeFieldName("value");
generator.writeRawValue(new String(data, CHARSET));
} else {
// Custom serializer uses byte[].
generator.writeBinaryField("value", data);
}
}
}
// End request object.
generator.writeEndObject();
// End operation object.
generator.writeEndObject();
}
// End array
generator.writeEndArray();
generator.close();
writer.flush();
payload = writer.toByteArray();
} catch (IOException e) {
e.printStackTrace();
return Mono.error(e);
}
if (count == 0) {
// No-op since there is no operation to be performed.
Mono.empty();
}
return this.daprClient.saveActorStateTransactionally(actorType, actorId.toString(), payload);
}
@Override
public int serializeOnJackson(Bean81S object, JsonGenerator jacksonSerializer) throws Exception {
jacksonSerializer.writeStartObject();
int fieldCount=0;
// Serialized Field:
// field id (mapped with "id")
fieldCount++;
jacksonSerializer.writeNumberField("id", object.id);
// field valueByteArray (mapped with "valueByteArray")
if (object.valueByteArray!=null) {
fieldCount++;
jacksonSerializer.writeBinaryField("valueByteArray", object.valueByteArray);
}
// field valueInteger (mapped with "valueInteger")
if (object.valueInteger!=null) {
fieldCount++;
jacksonSerializer.writeNumberField("valueInteger", object.valueInteger);
}
// field valueMapStringInteger (mapped with "valueMapStringInteger")
if (object.valueMapStringInteger!=null) {
fieldCount++;
// write wrapper tag
if (object.valueMapStringInteger.size()>0) {
jacksonSerializer.writeFieldName("valueMapStringInteger");
jacksonSerializer.writeStartArray();
for (Map.Entry<String, Integer> item: object.valueMapStringInteger.entrySet()) {
jacksonSerializer.writeStartObject();
jacksonSerializer.writeStringField("key", item.getKey());
if (item.getValue()==null) {
jacksonSerializer.writeNullField("value");
} else {
jacksonSerializer.writeNumberField("value", item.getValue());
}
jacksonSerializer.writeEndObject();
}
jacksonSerializer.writeEndArray();
} else {
jacksonSerializer.writeNullField("valueMapStringInteger");
}
}
jacksonSerializer.writeEndObject();
return fieldCount;
}
@Override
public int serializeOnJackson(Contact object, JsonGenerator jacksonSerializer) throws Exception {
jacksonSerializer.writeStartObject();
int fieldCount=0;
// Serialized Field:
// field birthDay (mapped with "birthDay")
if (object.birthDay!=null) {
fieldCount++;
// using type adapter sqlite.feature.typeadapter.DateAdapterType
jacksonSerializer.writeNumberField("birthDay", TypeAdapterUtils.toData(DateAdapterType.class, object.birthDay));
}
// field id (mapped with "id")
fieldCount++;
jacksonSerializer.writeNumberField("id", object.getId());
// field password (mapped with "password")
if (object.getPassword()!=null) {
fieldCount++;
jacksonSerializer.writeBinaryField("password", TypeAdapterUtils.toData(PasswordAdapterType.class, object.getPassword()));
}
// field surname (mapped with "surname")
if (object.surname!=null) {
fieldCount++;
jacksonSerializer.writeStringField("surname", object.surname);
}
// field type (mapped with "type")
if (object.type!=null) {
fieldCount++;
// using type adapter sqlite.feature.typeadapter.EnumAdapterType
jacksonSerializer.writeNumberField("type", TypeAdapterUtils.toData(EnumAdapterType.class, object.type));
}
// field updateDate (mapped with "updateDate")
if (object.updateDate!=null) {
fieldCount++;
jacksonSerializer.writeStringField("updateDate", SQLDateUtils.write(object.updateDate));
}
// field updateTime (mapped with "updateTime")
if (object.updateTime!=null) {
fieldCount++;
jacksonSerializer.writeStringField("updateTime", SQLTimeUtils.write(object.updateTime));
}
jacksonSerializer.writeEndObject();
return fieldCount;
}
@Override
public int serializeOnJackson(Bean05 object, JsonGenerator jacksonSerializer) throws Exception {
jacksonSerializer.writeStartObject();
int fieldCount=0;
// Serialized Field:
// field beanType (mapped with "beanType")
if (object.getBeanType()!=null) {
fieldCount++;
jacksonSerializer.writeStringField("beanType", object.getBeanType().toString());
}
// field content (mapped with "content")
if (object.getContent()!=null) {
fieldCount++;
jacksonSerializer.writeBinaryField("content", object.getContent());
}
// field creationTime (mapped with "creationTime")
if (object.getCreationTime()!=null) {
fieldCount++;
jacksonSerializer.writeStringField("creationTime", DateUtils.write(object.getCreationTime()));
}
// field number (mapped with "number")
fieldCount++;
jacksonSerializer.writeNumberField("number", object.getNumber());
// field pk (mapped with "pk")
fieldCount++;
jacksonSerializer.writeNumberField("pk", object.getPk());
// field text (mapped with "text")
if (object.getText()!=null) {
fieldCount++;
jacksonSerializer.writeStringField("text", object.getText());
}
jacksonSerializer.writeEndObject();
return fieldCount;
}
private void dumpNode(JsonGenerator jgen, String path, Stat stat, List<ACL> acls, byte[] data) throws IOException {
jgen.writeObjectFieldStart(path);
// The number of changes to the ACL of this znode.
jgen.writeNumberField(FIELD_AVERSION, stat.getAversion());
// The time in milliseconds from epoch when this znode was created.
jgen.writeNumberField(FIELD_CTIME, stat.getCtime());
// The number of changes to the children of this znode.
jgen.writeNumberField(FIELD_CVERSION, stat.getCversion());
// The zxid of the change that caused this znode to be created.
jgen.writeNumberField(FIELD_CZXID, stat.getCzxid());
// The length of the data field of this znode.
// jgen.writeNumberField("dataLength", stat.getDataLength());
// The session id of the owner of this znode if the znode is an ephemeral node. If it is not an ephemeral node,
// it will be zero.
jgen.writeNumberField(FIELD_EPHEMERAL_OWNER, stat.getEphemeralOwner());
// The time in milliseconds from epoch when this znode was last modified.
jgen.writeNumberField(FIELD_MTIME, stat.getMtime());
// The zxid of the change that last modified this znode.
jgen.writeNumberField(FIELD_MZXID, stat.getMzxid());
// The number of children of this znode.
jgen.writeNumberField("numChildren", stat.getNumChildren());
// last modified children?
jgen.writeNumberField(FIELD_PZXID, stat.getPzxid());
// The number of changes to the data of this znode.
jgen.writeNumberField(FIELD_VERSION, stat.getVersion());
if (data != null) {
jgen.writeBinaryField(FIELD_DATA, data);
} else {
jgen.writeNullField(FIELD_DATA);
}
jgen.writeArrayFieldStart(FIELD_ACLS);
for (ACL acl : acls) {
jgen.writeStartObject();
jgen.writeStringField(FIELD_ACL_ID, acl.getId().getId());
jgen.writeStringField(FIELD_ACL_SCHEME, acl.getId().getScheme());
jgen.writeNumberField(FIELD_ACL_PERMS, acl.getPerms());
jgen.writeEndObject();
}
jgen.writeEndArray();
jgen.writeEndObject();
}
@Override
public int serializeOnJackson(Bean81R object, JsonGenerator jacksonSerializer) throws Exception {
jacksonSerializer.writeStartObject();
int fieldCount=0;
// Serialized Field:
// field id (mapped with "id")
fieldCount++;
jacksonSerializer.writeNumberField("id", object.id);
// field valueByteArray (mapped with "valueByteArray")
if (object.valueByteArray!=null) {
fieldCount++;
jacksonSerializer.writeBinaryField("valueByteArray", object.valueByteArray);
}
// field valueInteger (mapped with "valueInteger")
if (object.valueInteger!=null) {
fieldCount++;
jacksonSerializer.writeNumberField("valueInteger", object.valueInteger);
}
// field valueMapStringInteger (mapped with "valueMapStringInteger")
if (object.valueMapStringInteger!=null) {
fieldCount++;
// write wrapper tag
if (object.valueMapStringInteger.size()>0) {
jacksonSerializer.writeFieldName("valueMapStringInteger");
jacksonSerializer.writeStartArray();
for (Map.Entry<String, Integer> item: object.valueMapStringInteger.entrySet()) {
jacksonSerializer.writeStartObject();
jacksonSerializer.writeStringField("key", item.getKey());
if (item.getValue()==null) {
jacksonSerializer.writeNullField("value");
} else {
jacksonSerializer.writeNumberField("value", item.getValue());
}
jacksonSerializer.writeEndObject();
}
jacksonSerializer.writeEndArray();
} else {
jacksonSerializer.writeNullField("valueMapStringInteger");
}
}
jacksonSerializer.writeEndObject();
return fieldCount;
}
@Override
public int serializeOnJacksonAsString(ByteBean object, JsonGenerator jacksonSerializer) throws
Exception {
jacksonSerializer.writeStartObject();
int fieldCount=0;
// Serialized Field:
// field id (mapped with "id")
jacksonSerializer.writeStringField("id", PrimitiveUtils.writeLong(object.id));
// field value (mapped with "value")
if (object.value!=null) {
fieldCount++;
jacksonSerializer.writeBinaryField("value", object.value);
}
// field value2 (mapped with "value2")
if (object.value2!=null) {
fieldCount++;
int n=object.value2.length;
Byte item;
// write wrapper tag
jacksonSerializer.writeFieldName("value2");
if (n>0) {
jacksonSerializer.writeStartArray();
for (int i=0; i<n; i++) {
item=object.value2[i];
if (item==null) {
jacksonSerializer.writeString("null");
} else {
jacksonSerializer.writeString(PrimitiveUtils.writeByte(item));
}
}
jacksonSerializer.writeEndArray();
} else {
jacksonSerializer.writeString("");
}
}
jacksonSerializer.writeEndObject();
return fieldCount;
}
@Override
public int serializeOnJacksonAsString(Bean81S object, JsonGenerator jacksonSerializer) throws
Exception {
jacksonSerializer.writeStartObject();
int fieldCount=0;
// Serialized Field:
// field id (mapped with "id")
jacksonSerializer.writeStringField("id", PrimitiveUtils.writeLong(object.id));
// field valueByteArray (mapped with "valueByteArray")
if (object.valueByteArray!=null) {
fieldCount++;
jacksonSerializer.writeBinaryField("valueByteArray", object.valueByteArray);
}
// field valueInteger (mapped with "valueInteger")
if (object.valueInteger!=null) {
jacksonSerializer.writeStringField("valueInteger", PrimitiveUtils.writeInteger(object.valueInteger));
}
// field valueMapStringInteger (mapped with "valueMapStringInteger")
if (object.valueMapStringInteger!=null) {
fieldCount++;
// write wrapper tag
if (object.valueMapStringInteger.size()>0) {
jacksonSerializer.writeFieldName("valueMapStringInteger");
jacksonSerializer.writeStartArray();
for (Map.Entry<String, Integer> item: object.valueMapStringInteger.entrySet()) {
jacksonSerializer.writeStartObject();
jacksonSerializer.writeStringField("key", item.getKey());
if (item.getValue()==null) {
jacksonSerializer.writeStringField("value", "null");
} else {
jacksonSerializer.writeStringField("value", PrimitiveUtils.writeInteger(item.getValue()));
}
jacksonSerializer.writeEndObject();
}
jacksonSerializer.writeEndArray();
} else {
jacksonSerializer.writeStringField("valueMapStringInteger", "null");
}
}
jacksonSerializer.writeEndObject();
return fieldCount;
}
@Override
public int serializeOnJacksonAsString(ByteBean object, JsonGenerator jacksonSerializer) throws
Exception {
jacksonSerializer.writeStartObject();
int fieldCount=0;
// Serialized Field:
// field id (mapped with "id")
jacksonSerializer.writeStringField("id", PrimitiveUtils.writeLong(object.getId()));
// field value (mapped with "value")
if (object.getValue()!=null) {
fieldCount++;
jacksonSerializer.writeBinaryField("value", object.getValue());
}
// field value2 (mapped with "value2")
if (object.getValue2()!=null) {
fieldCount++;
int n=object.getValue2().length;
Byte item;
// write wrapper tag
jacksonSerializer.writeFieldName("value2");
if (n>0) {
jacksonSerializer.writeStartArray();
for (int i=0; i<n; i++) {
item=object.getValue2()[i];
if (item==null) {
jacksonSerializer.writeString("null");
} else {
jacksonSerializer.writeString(PrimitiveUtils.writeByte(item));
}
}
jacksonSerializer.writeEndArray();
} else {
jacksonSerializer.writeString("");
}
}
jacksonSerializer.writeEndObject();
return fieldCount;
}
@Override
public int serializeOnJacksonAsString(Bean81R object, JsonGenerator jacksonSerializer) throws
Exception {
jacksonSerializer.writeStartObject();
int fieldCount=0;
// Serialized Field:
// field id (mapped with "id")
jacksonSerializer.writeStringField("id", PrimitiveUtils.writeLong(object.id));
// field valueByteArray (mapped with "valueByteArray")
if (object.valueByteArray!=null) {
fieldCount++;
jacksonSerializer.writeBinaryField("valueByteArray", object.valueByteArray);
}
// field valueInteger (mapped with "valueInteger")
if (object.valueInteger!=null) {
jacksonSerializer.writeStringField("valueInteger", PrimitiveUtils.writeInteger(object.valueInteger));
}
// field valueMapStringInteger (mapped with "valueMapStringInteger")
if (object.valueMapStringInteger!=null) {
fieldCount++;
// write wrapper tag
if (object.valueMapStringInteger.size()>0) {
jacksonSerializer.writeFieldName("valueMapStringInteger");
jacksonSerializer.writeStartArray();
for (Map.Entry<String, Integer> item: object.valueMapStringInteger.entrySet()) {
jacksonSerializer.writeStartObject();
jacksonSerializer.writeStringField("key", item.getKey());
if (item.getValue()==null) {
jacksonSerializer.writeStringField("value", "null");
} else {
jacksonSerializer.writeStringField("value", PrimitiveUtils.writeInteger(item.getValue()));
}
jacksonSerializer.writeEndObject();
}
jacksonSerializer.writeEndArray();
} else {
jacksonSerializer.writeStringField("valueMapStringInteger", "null");
}
}
jacksonSerializer.writeEndObject();
return fieldCount;
}