2626import com .fasterxml .jackson .databind .ser .impl .SimpleBeanPropertyFilter ;
2727import com .fasterxml .jackson .databind .ser .impl .SimpleFilterProvider ;
2828import com .qcloud .cos .exception .CosClientException ;
29+ import com .qcloud .cos .internal .CosServiceRequest ;
2930import org .slf4j .Logger ;
3031import org .slf4j .LoggerFactory ;
3132
32- import java .io .IOException ;
33- import java .io .Writer ;
33+ import java .lang .reflect .Field ;
34+ import java .nio .charset .StandardCharsets ;
35+ import java .util .HashSet ;
36+ import java .util .Set ;
3437
3538public enum CIJackson {
3639 ;
@@ -43,20 +46,26 @@ public enum CIJackson {
4346 objectMapper .configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false );
4447 objectMapper .disable (SerializationFeature .FAIL_ON_EMPTY_BEANS );
4548 objectMapper .setPropertyNamingStrategy (PropertyNamingStrategy .PASCAL_CASE_TO_CAMEL_CASE );
46- SimpleBeanPropertyFilter filter = SimpleBeanPropertyFilter .serializeAllExcept ();
49+ SimpleBeanPropertyFilter filter = SimpleBeanPropertyFilter .serializeAllExcept (getFieldsToFilter ( CosServiceRequest . class ) );
4750 FilterProvider filters = new SimpleFilterProvider ().addFilter ("CosServiceFilter" , filter );
4851 objectMapper .setFilterProvider (filters );
4952 }
50-
5153 private static final ObjectWriter writer = objectMapper .writer ();
52- private static final ObjectWriter prettyWriter = objectMapper .writerWithDefaultPrettyPrinter ();
5354
54- public static String toJsonPrettyString (Object value ) {
55- try {
56- return prettyWriter .writeValueAsString (value );
57- } catch (Exception e ) {
58- throw new IllegalStateException (e );
55+ private static Set <String > getFieldsToFilter (Class cosServiceRequestClass ) {
56+ Set <String > fieldNames = new HashSet <>();
57+ Field [] declaredFields = cosServiceRequestClass .getDeclaredFields ();
58+ for (Field declaredField : declaredFields ) {
59+ String name = declaredField .getName ();
60+ String fieldNameCapitalized = capitalizeFirstLetter (name );
61+ fieldNames .add (fieldNameCapitalized );
5962 }
63+ fieldNames .add ("ReadLimit" );
64+ fieldNames .add ("CloneRoot" );
65+ fieldNames .add ("GeneralProgressListener" );
66+ fieldNames .add ("RequestId" );
67+ fieldNames .add ("BucketName" );
68+ return fieldNames ;
6069 }
6170
6271 public static String toJsonString (Object value ) {
@@ -67,23 +76,19 @@ public static String toJsonString(Object value) {
6776 }
6877 }
6978
70- public static <T > T fromJsonString (String json , Class <T > clazz ) {
71- if (json == null )
72- return null ;
79+ public static byte [] toJsonBytes (Object value ) {
7380 try {
74- return objectMapper . readValue ( json , clazz );
81+ return writer . writeValueAsString ( value ). getBytes ( StandardCharsets . UTF_8 );
7582 } catch (Exception e ) {
76- throw new CosClientException ( "Unable to parse Json String." , e );
83+ throw new IllegalStateException ( e );
7784 }
7885 }
7986
80- public static JsonNode jsonNodeOf (String json ) {
81- return fromJsonString (json , JsonNode .class );
82- }
83-
84- public static JsonGenerator jsonGeneratorOf (Writer writer ) throws IOException {
85- return new JsonFactory ().createGenerator (writer );
87+ private static String capitalizeFirstLetter (String str ) {
88+ if (str == null || str .isEmpty ()) {
89+ return str ;
90+ }
91+ return Character .toUpperCase (str .charAt (0 )) + str .substring (1 );
8692 }
87-
8893}
8994
0 commit comments