java - 使用 Jackson 解压缩时出现 json 解析器异常

标签 java json parsing compression jackson

我正在使用JACKSON

User.Java

public class User {

private int age;
private String name;

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

}

对于压缩,我引用this article

package com.mkyong.core;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import org.apache.commons.io.IOUtils;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

 public class JacksonExample {

public static byte[] compress(byte[] content) {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    try {
        GZIPOutputStream gzipOutputStream = new GZIPOutputStream(
                byteArrayOutputStream);
        gzipOutputStream.write(content);
        gzipOutputStream.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    return byteArrayOutputStream.toByteArray();
}

public static byte[] decompress(byte[] data) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        IOUtils.copy(new GZIPInputStream(new ByteArrayInputStream(data)),
                out);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return out.toByteArray();
}

public static void main(String[] args) {

    User user = new User();
    user.setAge(25);
    user.setName("Shahid");

    ObjectMapper mapper = new ObjectMapper();
    StringOutputStream os = new StringOutputStream();
    try {

        System.out.println("//////////////////COMMPRESSION///////////////////");

        System.out.println("========Writing to OS======");
        mapper.writeValue(os, user);

        System.out.println("JSON ON OS======"+os.getString());

        System.out.println("=======Compressing the OS======");

        byte[] cos = compress(os.toString().getBytes());

        System.out.println("Compressed OS==="+cos.toString());

        System.out.println();
        System.out.println();


        System.out.println("//////////////////DECOMPRES///////////////////");

        InputStream is = null;
        is = new ByteArrayInputStream(decompress(cos));


        User user1 = mapper.readValue(decompress(cos), User.class);

        // display to console
        System.out.println(user1.getAge());
        System.out.println(user1.getName());

    } catch (JsonGenerationException e) {

        e.printStackTrace();

    } catch (JsonMappingException e) {

        e.printStackTrace();

    } catch (IOException e) {

        e.printStackTrace();

    }

}

}

输出异常

//////////////////COMMPRESSION///////////////////
========Writing to OS======
JSON ON OS======{"name":"Shahid","age":25}
=======Compressing the OS======
Compressed OS===[B@1327d5a0


//////////////////DECOMPRES///////////////////

org.codehaus.jackson.JsonParseException: Unexpected character ('c' (code 99)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
 at [Source: [B@6ce1d9a; line: 1, column: 2]
at org.codehaus.jackson.JsonParser._constructError(JsonParser.java:1291)
at org.codehaus.jackson.impl.JsonParserMinimalBase._reportError(JsonParserMinimalBase.java:385)
at org.codehaus.jackson.impl.JsonParserMinimalBase._reportUnexpectedChar(JsonParserMinimalBase.java:306)
at org.codehaus.jackson.impl.Utf8StreamParser._handleUnexpectedValue(Utf8StreamParser.java:1581)
at org.codehaus.jackson.impl.Utf8StreamParser._nextTokenNotInObject(Utf8StreamParser.java:436)
at org.codehaus.jackson.impl.Utf8StreamParser.nextToken(Utf8StreamParser.java:322)
at org.codehaus.jackson.map.ObjectMapper._initForReading(ObjectMapper.java:2432)
at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2389)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1667)
at com.mkyong.core.JacksonExample.main(JacksonExample.java:75)

最佳答案

此代码工作检查一次,使用 StringWriter

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.StringWriter;

import java.util.zip.GZIPInputStream;

import java.util.zip.GZIPOutputStream;

import org.apache.commons.io.IOUtils;

import org.codehaus.jackson.JsonGenerationException;

import org.codehaus.jackson.map.JsonMappingException;

import org.codehaus.jackson.map.ObjectMapper;

 public class JacksonExample {

public static byte[] compress(byte[] content) {

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    try {

        GZIPOutputStream gzipOutputStream = new GZIPOutputStream(
                byteArrayOutputStream);
        gzipOutputStream.write(content);
        gzipOutputStream.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    return byteArrayOutputStream.toByteArray();
}

public static byte[] decompress(byte[] data) throws IOException {

    ByteArrayOutputStream out = new ByteArrayOutputStream();

    try {
        IOUtils.copy(new GZIPInputStream(new ByteArrayInputStream(data)),
                out);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return out.toByteArray();
}

public static void main(String[] args) {


    User user = new User();
    user.setAge(25);
    user.setName("Shahid");

    ObjectMapper mapper = new ObjectMapper();
    StringWriter os = new StringWriter(); 

    try {

        System.out.println("//////////////////COMMPRESSION///////////////////");

        System.out.println("========Writing to OS======");
        mapper.writeValue(os, user);

        System.out.println("JSON ON OS======"+os);

        System.out.println("=======Compressing the OS======");

        byte[] cos = compress(os.toString().getBytes());

        System.out.println("Compressed OS==="+cos.toString());

        System.out.println();
        System.out.println();


        System.out.println("//////////////////DECOMPRES///////////////////");

        InputStream is = null;
        is = new ByteArrayInputStream(decompress(cos));


        User user1 = mapper.readValue(decompress(cos), User.class);

        // display to console
        System.out.println(user1.getAge());
        System.out.println(user1.getName());

    } catch (JsonGenerationException e) {

        e.printStackTrace();

    } catch (JsonMappingException e) {

        e.printStackTrace();

    } catch (IOException e) {

        e.printStackTrace();

    }

}

}

关于java - 使用 Jackson 解压缩时出现 json 解析器异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19648645/

相关文章:

java - Jackson JsonParser 获取数组 token

C:从 u_char* 解析子字符串

java - 简单XML : ConstructorException

json - 根据 JSON 模式验证结构

java - Andengine - 检测与多个对象的碰撞并删除它们 - Java

java - 为什么构造函数用@JsonCreator注解时,它的参数必须用@JsonProperty注解?

java - 在Java中将JSON的每个元素解析为数组

c - 在哪里可以找到实现编译器所需的完整 C 语法?

java - "(?i)"不适用于重音

java - 背景 : url(images/2. png)在 style.css 中不起作用