本文整理汇总了Java中com.fasterxml.jackson.databind.ObjectMapper.writeValueAsBytes方法的典型用法代码示例。如果您正苦于以下问题:Java ObjectMapper.writeValueAsBytes方法的具体用法?Java ObjectMapper.writeValueAsBytes怎么用?Java ObjectMapper.writeValueAsBytes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.fasterxml.jackson.databind.ObjectMapper
的用法示例。
在下文中一共展示了ObjectMapper.writeValueAsBytes方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: convertObjectToJsonBytes
import com.fasterxml.jackson.databind.ObjectMapper; //导入方法依赖的package包/类
/**
* Convert an object to JSON byte array.
*
* @param object
* the object to convert
* @return the JSON byte array
* @throws IOException
*/
public static byte[] convertObjectToJsonBytes(Object object)
throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
JavaTimeModule module = new JavaTimeModule();
module.addSerializer(OffsetDateTime.class, JSR310DateTimeSerializer.INSTANCE);
module.addSerializer(ZonedDateTime.class, JSR310DateTimeSerializer.INSTANCE);
module.addSerializer(LocalDateTime.class, JSR310DateTimeSerializer.INSTANCE);
module.addSerializer(Instant.class, JSR310DateTimeSerializer.INSTANCE);
module.addDeserializer(LocalDate.class, JSR310LocalDateDeserializer.INSTANCE);
mapper.registerModule(module);
return mapper.writeValueAsBytes(object);
}
示例2: asEvent
import com.fasterxml.jackson.databind.ObjectMapper; //导入方法依赖的package包/类
public InputEvent asEvent() {
ObjectMapper objectMapper = new ObjectMapper();
byte[] body;
try {
body = objectMapper.writeValueAsBytes(req);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
System.err.println("Req:" + new String(body));
return new InputEventBuilder()
.withHeader(FLOW_ID_HEADER, flowId)
.withHeader("Content-type", "application/json")
.withBody(new ByteArrayInputStream(body))
.build();
}
示例3: convertObjectToJsonBytes
import com.fasterxml.jackson.databind.ObjectMapper; //导入方法依赖的package包/类
/**
* Convert an object to JSON byte array.
*
* @param object
* the object to convert
* @return the JSON byte array
* @throws IOException
*/
public static byte[] convertObjectToJsonBytes(Object object)
throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
JavaTimeModule module = new JavaTimeModule();
mapper.registerModule(module);
return mapper.writeValueAsBytes(object);
}
示例4: serializeNullValue
import com.fasterxml.jackson.databind.ObjectMapper; //导入方法依赖的package包/类
@Test
public void serializeNullValue() throws IOException {
final TestEntityString entity = new TestEntityString();
entity.setS(null);
final ObjectMapper mapper = new VPackMapper();
mapper.setSerializationInclusion(Include.ALWAYS);
final VPackSlice vpack = new VPackSlice(mapper.writeValueAsBytes(entity));
assertThat(vpack, is(notNullValue()));
final VPackSlice s = vpack.get("s");
assertThat(s.isNull(), is(true));
}
示例5: convertObjectToJsonBytes
import com.fasterxml.jackson.databind.ObjectMapper; //导入方法依赖的package包/类
/**
* Convert an object to JSON byte array.
*
* @param object the object to convert
* @return the JSON byte array
* @throws IOException
*/
public static byte[] convertObjectToJsonBytes(Object object)
throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
JavaTimeModule module = new JavaTimeModule();
mapper.registerModule(module);
return mapper.writeValueAsBytes(object);
}
示例6: main
import com.fasterxml.jackson.databind.ObjectMapper; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException {
AceApplicationConfig aceApplicationConfig=new AceApplicationConfig();
ObjectMapper objectMapper=new ObjectMapper();
byte[] bytes=objectMapper.writeValueAsBytes(aceApplicationConfig);
//BeanSerializer jsonSerializer=new BeanSerializer();
aceApplicationConfig= objectMapper.readValue(bytes,aceApplicationConfig.getClass());
System.out.println(aceApplicationConfig.getPackages());
Pattern p = Pattern.compile(".?.html|.jpg|.png|.css|.js");
System.out.println(p.matcher("a.html").find());
System.out.println(p.matcher("a.htm").find());
System.out.println(p.matcher("a.css").find());
System.out.println(p.matcher("a.js").find());
System.out.println(TestChar.class.getClass());
System.out.println(TestChar.class);
LocalDateTime now=LocalDateTime.now();
LocalDateTime dateTime= LocalDateTime.of(2017,4,19,6,6);
System.out.println(now);
System.out.println(dateTime);
System.out.println(now.toLocalDate().equals(dateTime.toLocalDate()));
char a=(char) 35;
System.out.println(a);
a=(char) 61;
System.out.println(a);
}
示例7: initTypeInclusion
import com.fasterxml.jackson.databind.ObjectMapper; //导入方法依赖的package包/类
protected void initTypeInclusion(ObjectMapper mapObjectMapper) {
TypeResolverBuilder<?> mapTyper = new DefaultTypeResolverBuilder(DefaultTyping.NON_FINAL) {
public boolean useForType(JavaType t) {
switch (_appliesFor) {
case NON_CONCRETE_AND_ARRAYS:
while (t.isArrayType()) {
t = t.getContentType();
}
// fall through
case OBJECT_AND_NON_CONCRETE:
return (t.getRawClass() == Object.class) || !t.isConcrete();
case NON_FINAL:
while (t.isArrayType()) {
t = t.getContentType();
}
// to fix problem with wrong long to int conversion
if (t.getRawClass() == Long.class) {
return true;
}
if (t.getRawClass() == XMLGregorianCalendar.class) {
return false;
}
return !t.isFinal(); // includes Object.class
default:
// case JAVA_LANG_OBJECT:
return (t.getRawClass() == Object.class);
}
}
};
mapTyper.init(JsonTypeInfo.Id.CLASS, null);
mapTyper.inclusion(JsonTypeInfo.As.PROPERTY);
mapObjectMapper.setDefaultTyping(mapTyper);
// warm up codec
try {
byte[] s = mapObjectMapper.writeValueAsBytes(1);
mapObjectMapper.readValue(s, Object.class);
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
示例8: convertObjectToJsonBytes
import com.fasterxml.jackson.databind.ObjectMapper; //导入方法依赖的package包/类
public static byte[] convertObjectToJsonBytes(Object object) throws IOException {
ObjectMapper om = new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);
return om.writeValueAsBytes(object);
}
示例9: writeToGameEngineActivity
import com.fasterxml.jackson.databind.ObjectMapper; //导入方法依赖的package包/类
public boolean writeToGameEngineActivity(int attempt, String user, String pass, String code) {
if (attempt > 5) {
KRFAM.Toast("Failed to write to '" + codeName + "' A.D File.\nHave you allowed KRFAM to have root access?");
return false;
}
if (installed == true) {
SaveData value = new SaveData();
value.m_UUID = user;
value.m_AccessToken = pass;
value.m_MyCode = code;
value.m_ConfirmedVer = 0;
String _iv = createPassword();
byte[] iv = _iv.getBytes();
KRFAM.log("iv " + _iv);
try {
MessagePack.PackerConfig config = new MessagePack.PackerConfig().withStr8FormatSupport(false);
ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory(config));
byte[] bytes = objectMapper.writeValueAsBytes(value);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
SecretKeySpec skeySpec = new SecretKeySpec(this.EncryptKey.getBytes(), "AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(iv));
byte[] encrypted = cipher.doFinal(bytes);
OutputStream is = new FileOutputStream(SaveFile);
LittleEndianDataOutputStream binaryWriter = new LittleEndianDataOutputStream(is);
int num1 = new Random().nextInt();
byte num2 = (byte) (num1 & 127);
int num3 = (int) ((long) num1 & 4294902015L) | 65280 & 19 << 8; // 19 denotes 171101
/* 16 - 20: _170404, _170713, _171101, _latest, */
binaryWriter.writeInt(num3);
for (int index = 0; index < iv.length; ++index) iv[index] += (byte) (96 + (int) (byte) index);
binaryWriter.writeByte((byte) ((int) iv.length + (int) num2));
binaryWriter.write(iv);
binaryWriter.writeInt(encrypted.length);
binaryWriter.write(encrypted);
} catch (Exception ex) {
KRFAM.log(ex);
KRFAM.log(codeName + " > Failed to Write A.D File");
KRFAM.forcePermission(adFile);
KRFAM.forcePermission(adFile.getParentFile());
}
KRFAM.log("saved uu " + uuid);
KRFAM.log("saved at " + accessToken);
updateFromADFile();
if (uuid.equals(user) && accessToken.equals(pass)) {
return true;
} else {
KRFAM.forcePermission(adFile);
KRFAM.forcePermission(adFile.getParentFile());
return writeToGameEngineActivity(attempt + 1, user, pass, code);
}
} else {
KRFAM.Toast(codeName + " not installed");
return false;
}
}
示例10: encode
import com.fasterxml.jackson.databind.ObjectMapper; //导入方法依赖的package包/类
/**
* Encode.
*
* @param json the json
* @return the byte[]
* @throws IOException Signals that an I/O exception has occurred.
*/
public byte[] encode(JsonNode json) throws IOException{
ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsBytes(json);
}