2013-10-29 47 views
0

解压缩,我使用JACKSONJSON解析器异常,同时使用杰克逊

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===[[email protected] 


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

org.codehaus.jackson.JsonParseException: Unexpected character ('c' (code 99)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') 
at [Source: [[email protected]; 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) 

回答

1

这个代码是一次工作检查,可使用的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(); 

    } 

} 

}