コード例 #1
0
  /**
   * Method called to serialize fields, when the value type is statically known, so that value
   * serializer is passed and does not need to be fetched from provider.
   */
  public void serializeFieldsUsing(
      Map<?, ?> value, JsonGenerator gen, SerializerProvider provider, JsonSerializer<Object> ser)
      throws IOException {
    final JsonSerializer<Object> keySerializer = _keySerializer;
    final Set<String> ignored = _ignoredEntries;
    final TypeSerializer typeSer = _valueTypeSerializer;

    for (Map.Entry<?, ?> entry : value.entrySet()) {
      Object keyElem = entry.getKey();
      if (ignored != null && ignored.contains(keyElem)) continue;

      if (keyElem == null) {
        provider.findNullKeySerializer(_keyType, _property).serialize(null, gen, provider);
      } else {
        keySerializer.serialize(keyElem, gen, provider);
      }
      final Object valueElem = entry.getValue();
      if (valueElem == null) {
        provider.defaultSerializeNull(gen);
      } else {
        try {
          if (typeSer == null) {
            ser.serialize(valueElem, gen, provider);
          } else {
            ser.serializeWithType(valueElem, gen, provider, typeSer);
          }
        } catch (Exception e) {
          wrapAndThrow(provider, e, value, String.valueOf(keyElem));
        }
      }
    }
  }
コード例 #2
0
  protected void _writeNullKeyedEntry(JsonGenerator gen, SerializerProvider provider, Object value)
      throws IOException {
    JsonSerializer<Object> keySerializer = provider.findNullKeySerializer(_keyType, _property);
    JsonSerializer<Object> valueSer;
    if (value == null) {
      if (_suppressNulls) {
        return;
      }
      valueSer = provider.getDefaultNullValueSerializer();
    } else {
      valueSer = _valueSerializer;
      if (valueSer == null) {
        valueSer = _findSerializer(provider, value);
      }
      if (_suppressableValue == MARKER_FOR_EMPTY) {
        if (valueSer.isEmpty(provider, value)) {
          return;
        }
      } else if ((_suppressableValue != null) && (_suppressableValue.equals(value))) {
        return;
      }
    }

    try {
      keySerializer.serialize(null, gen, provider);
      valueSer.serialize(value, gen, provider);
    } catch (Exception e) {
      wrapAndThrow(provider, e, value, "");
    }
  }
コード例 #3
0
 public void serializeTypedContents(
     Object[] paramArrayOfObject,
     JsonGenerator paramJsonGenerator,
     SerializerProvider paramSerializerProvider)
     throws IOException {
   int k = paramArrayOfObject.length;
   TypeSerializer localTypeSerializer = this._valueTypeSerializer;
   int j = 0;
   int i = 0;
   Object localObject1 = null;
   for (; ; ) {
     try {
       PropertySerializerMap localPropertySerializerMap = this._dynamicSerializers;
       if (i < k) {
         Object localObject2 = paramArrayOfObject[i];
         if (localObject2 == null) {
           localObject1 = localObject2;
           j = i;
           paramSerializerProvider.defaultSerializeNull(paramJsonGenerator);
         } else {
           localObject1 = localObject2;
           j = i;
           Class localClass = localObject2.getClass();
           localObject1 = localObject2;
           j = i;
           JsonSerializer localJsonSerializer2 =
               localPropertySerializerMap.serializerFor(localClass);
           JsonSerializer localJsonSerializer1 = localJsonSerializer2;
           if (localJsonSerializer2 == null) {
             localObject1 = localObject2;
             j = i;
             localJsonSerializer1 =
                 _findAndAddDynamic(
                     localPropertySerializerMap, localClass, paramSerializerProvider);
           }
           localObject1 = localObject2;
           j = i;
           localJsonSerializer1.serializeWithType(
               localObject2, paramJsonGenerator, paramSerializerProvider, localTypeSerializer);
         }
       }
     } catch (IOException paramArrayOfObject) {
       throw paramArrayOfObject;
     } catch (Exception paramArrayOfObject) {
       if (((paramArrayOfObject instanceof InvocationTargetException))
           && (paramArrayOfObject.getCause() != null)) {
         paramArrayOfObject = paramArrayOfObject.getCause();
         continue;
       }
       if ((paramArrayOfObject instanceof Error)) {
         throw ((Error) paramArrayOfObject);
       }
       throw JsonMappingException.wrapWithPath(paramArrayOfObject, localObject1, j);
     }
     return;
     i += 1;
   }
 }
コード例 #4
0
  /**
   * Helper method used when we have a JSON Filter to use for potentially filtering out Map entries.
   *
   * @since 2.5
   */
  public void serializeFilteredFields(
      Map<?, ?> value,
      JsonGenerator gen,
      SerializerProvider provider,
      PropertyFilter filter,
      Object suppressableValue) // since 2.5
      throws IOException {
    final Set<String> ignored = _ignoredEntries;
    final MapProperty prop = new MapProperty(_valueTypeSerializer, _property);
    final boolean checkEmpty = (MARKER_FOR_EMPTY == suppressableValue);

    for (Map.Entry<?, ?> entry : value.entrySet()) {
      // First, serialize key; unless ignorable by key
      final Object keyElem = entry.getKey();
      if (ignored != null && ignored.contains(keyElem)) continue;

      JsonSerializer<Object> keySerializer;
      if (keyElem == null) {
        keySerializer = provider.findNullKeySerializer(_keyType, _property);
      } else {
        keySerializer = _keySerializer;
      }
      // or by value; nulls often suppressed
      final Object valueElem = entry.getValue();

      JsonSerializer<Object> valueSer;
      // And then value
      if (valueElem == null) {
        if (_suppressNulls) {
          continue;
        }
        valueSer = provider.getDefaultNullValueSerializer();
      } else {
        valueSer = _valueSerializer;
        if (valueSer == null) {
          valueSer = _findSerializer(provider, valueElem);
        }
        // also may need to skip non-empty values:
        if (checkEmpty) {
          if (valueSer.isEmpty(provider, valueElem)) {
            continue;
          }
        } else if (suppressableValue != null) {
          if (suppressableValue.equals(valueElem)) {
            continue;
          }
        }
      }
      // and with that, ask filter to handle it
      prop.reset(keyElem, keySerializer, valueSer);
      try {
        filter.serializeAsField(valueElem, gen, provider, prop);
      } catch (Exception e) {
        wrapAndThrow(provider, e, value, String.valueOf(keyElem));
      }
    }
  }
コード例 #5
0
  @Override
  public boolean isEmpty(SerializerProvider prov, Map<?, ?> value) {
    if (value.isEmpty()) {
      return true;
    }

    // 05-Nove-2015, tatu: Simple cases are cheap, but for recursive
    //   emptiness checking we actually need to see if values are empty as well.
    Object supp = _suppressableValue;
    if ((supp == null) && !_suppressNulls) {
      return false;
    }
    JsonSerializer<Object> valueSer = _valueSerializer;
    final boolean checkEmpty = (MARKER_FOR_EMPTY == supp);
    if (valueSer != null) {
      for (Object elemValue : value.values()) {
        if (elemValue == null) {
          if (_suppressNulls) {
            continue;
          }
          return false;
        }
        if (checkEmpty) {
          if (!valueSer.isEmpty(prov, elemValue)) {
            return false;
          }
        } else if ((supp == null) || !supp.equals(value)) {
          return false;
        }
      }
      return true;
    }
    // But if not statically known, try this:
    for (Object elemValue : value.values()) {
      if (elemValue == null) {
        if (_suppressNulls) {
          continue;
        }
        return false;
      }
      try {
        valueSer = _findSerializer(prov, elemValue);
      } catch (JsonMappingException e) { // Ugh... can not just throw as-is, so...
        // 05-Nov-2015, tatu: For now, probably best not to assume empty then
        return false;
      }
      if (checkEmpty) {
        if (!valueSer.isEmpty(prov, elemValue)) {
          return false;
        }
      } else if ((supp == null) || !supp.equals(value)) {
        return false;
      }
    }
    return true;
  }
コード例 #6
0
  /** Serialization method called when exclusion filtering needs to be applied. */
  public void serializeOptionalFields(
      Map<?, ?> value, JsonGenerator gen, SerializerProvider provider, Object suppressableValue)
      throws IOException {
    // If value type needs polymorphic type handling, some more work needed:
    if (_valueTypeSerializer != null) {
      serializeTypedFields(value, gen, provider, suppressableValue);
      return;
    }
    final Set<String> ignored = _ignoredEntries;
    final boolean checkEmpty = (MARKER_FOR_EMPTY == suppressableValue);

    for (Map.Entry<?, ?> entry : value.entrySet()) {
      // First find key serializer
      final Object keyElem = entry.getKey();
      JsonSerializer<Object> keySerializer;
      if (keyElem == null) {
        keySerializer = provider.findNullKeySerializer(_keyType, _property);
      } else {
        if (ignored != null && ignored.contains(keyElem)) continue;
        keySerializer = _keySerializer;
      }

      // Then value serializer
      final Object valueElem = entry.getValue();
      JsonSerializer<Object> valueSer;
      if (valueElem == null) {
        if (_suppressNulls) { // all suppressions include null-suppression
          continue;
        }
        valueSer = provider.getDefaultNullValueSerializer();
      } else {
        valueSer = _valueSerializer;
        if (valueSer == null) {
          valueSer = _findSerializer(provider, valueElem);
        }
        // also may need to skip non-empty values:
        if (checkEmpty) {
          if (valueSer.isEmpty(provider, valueElem)) {
            continue;
          }
        } else if (suppressableValue != null) {
          if (suppressableValue.equals(valueElem)) {
            continue;
          }
        }
      }
      // and then serialize, if all went well
      try {
        keySerializer.serialize(keyElem, gen, provider);
        valueSer.serialize(valueElem, gen, provider);
      } catch (Exception e) {
        wrapAndThrow(provider, e, value, String.valueOf(keyElem));
      }
    }
  }
コード例 #7
0
ファイル: MapSerializer.java プロジェクト: chenhn/joyplus-tv
  /** Method called to serialize fields, when the value type is not statically known. */
  public void serializeFields(Map<?, ?> value, JsonGenerator jgen, SerializerProvider provider)
      throws IOException, JsonGenerationException {
    // If value type needs polymorphic type handling, some more work needed:
    if (_valueTypeSerializer != null) {
      serializeTypedFields(value, jgen, provider);
      return;
    }
    final JsonSerializer<Object> keySerializer = _keySerializer;

    final HashSet<String> ignored = _ignoredEntries;
    final boolean skipNulls = !provider.isEnabled(SerializationFeature.WRITE_NULL_MAP_VALUES);

    PropertySerializerMap serializers = _dynamicValueSerializers;

    for (Map.Entry<?, ?> entry : value.entrySet()) {
      Object valueElem = entry.getValue();
      // First, serialize key
      Object keyElem = entry.getKey();
      if (keyElem == null) {
        provider.findNullKeySerializer(_keyType, _property).serialize(null, jgen, provider);
      } else {
        // [JACKSON-314] skip entries with null values?
        if (skipNulls && valueElem == null) continue;
        // One twist: is entry ignorable? If so, skip
        if (ignored != null && ignored.contains(keyElem)) continue;
        keySerializer.serialize(keyElem, jgen, provider);
      }

      // And then value
      if (valueElem == null) {
        provider.defaultSerializeNull(jgen);
      } else {
        Class<?> cc = valueElem.getClass();
        JsonSerializer<Object> serializer = serializers.serializerFor(cc);
        if (serializer == null) {
          if (_valueType.hasGenericTypes()) {
            serializer =
                _findAndAddDynamic(
                    serializers, provider.constructSpecializedType(_valueType, cc), provider);
          } else {
            serializer = _findAndAddDynamic(serializers, cc, provider);
          }
          serializers = _dynamicValueSerializers;
        }
        try {
          serializer.serialize(valueElem, jgen, provider);
        } catch (Exception e) {
          // [JACKSON-55] Need to add reference information
          String keyDesc = "" + keyElem;
          wrapAndThrow(provider, e, value, keyDesc);
        }
      }
    }
  }
コード例 #8
0
 /** Helper method that encapsulates logic of retrieving and caching required serializer. */
 protected final JsonSerializer<Object> _findCachedSerializer(
     SerializerProvider provider, Class<?> type) throws JsonMappingException {
   JsonSerializer<Object> ser = _dynamicSerializers.serializerFor(type);
   if (ser == null) {
     ser = _findSerializer(provider, type, _property);
     if (_unwrapper != null) {
       ser = ser.unwrappingSerializer(_unwrapper);
     }
     _dynamicSerializers = _dynamicSerializers.newWith(type, ser);
   }
   return ser;
 }
コード例 #9
0
 @Override
 public JsonSerializer<Optional<?>> unwrappingSerializer(NameTransformer transformer) {
   JsonSerializer<Object> ser = _valueSerializer;
   if (ser != null) {
     ser = ser.unwrappingSerializer(transformer);
   }
   NameTransformer unwrapper =
       (_unwrapper == null)
           ? transformer
           : NameTransformer.chainedTransformer(transformer, _unwrapper);
   return withResolved(_property, _valueTypeSerializer, ser, unwrapper, _contentInclusion);
 }
コード例 #10
0
  /** @since 2.5 */
  public void serializeTypedFields(
      Map<?, ?> value,
      JsonGenerator gen,
      SerializerProvider provider,
      Object suppressableValue) // since 2.5
      throws IOException {
    final Set<String> ignored = _ignoredEntries;
    final boolean checkEmpty = (MARKER_FOR_EMPTY == suppressableValue);

    for (Map.Entry<?, ?> entry : value.entrySet()) {
      Object keyElem = entry.getKey();
      JsonSerializer<Object> keySerializer;
      if (keyElem == null) {
        keySerializer = provider.findNullKeySerializer(_keyType, _property);
      } else {
        // One twist: is entry ignorable? If so, skip
        if (ignored != null && ignored.contains(keyElem)) continue;
        keySerializer = _keySerializer;
      }
      final Object valueElem = entry.getValue();

      // And then value
      JsonSerializer<Object> valueSer;
      if (valueElem == null) {
        if (_suppressNulls) { // all suppression include null suppression
          continue;
        }
        valueSer = provider.getDefaultNullValueSerializer();
      } else {
        valueSer = _valueSerializer;
        if (valueSer == null) {
          valueSer = _findSerializer(provider, valueElem);
        }
        // also may need to skip non-empty values:
        if (checkEmpty) {
          if (valueSer.isEmpty(provider, valueElem)) {
            continue;
          }
        } else if (suppressableValue != null) {
          if (suppressableValue.equals(valueElem)) {
            continue;
          }
        }
      }
      keySerializer.serialize(keyElem, gen, provider);
      try {
        valueSer.serializeWithType(valueElem, gen, provider, _valueTypeSerializer);
      } catch (Exception e) {
        wrapAndThrow(provider, e, value, String.valueOf(keyElem));
      }
    }
  }
コード例 #11
0
 /**
  * Configure custom serializers. Each serializer is registered for the type returned by {@link
  * JsonSerializer#handledType()}, which must not be {@code null}.
  *
  * @see #serializersByType(Map)
  */
 public Jackson2ObjectMapperBuilder serializers(JsonSerializer<?>... serializers) {
   if (serializers != null) {
     for (JsonSerializer<?> serializer : serializers) {
       Class<?> handledType = serializer.handledType();
       if (handledType == null || handledType == Object.class) {
         throw new IllegalArgumentException(
             "Unknown handled type in " + serializer.getClass().getName());
       }
       this.serializers.put(serializer.handledType(), serializer);
     }
   }
   return this;
 }
コード例 #12
0
 @Override
 public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint)
     throws JsonMappingException {
   JsonSerializer<?> ser = _valueSerializer;
   if (ser == null) {
     // 28-Sep-2015, tatu: as per [datatype-guava#83] need to ensure we don't
     //    accidentally drop parameterization
     ser = _findSerializer(visitor.getProvider(), _referredType, _property);
     if (_unwrapper != null) {
       ser = ser.unwrappingSerializer(_unwrapper);
     }
   }
   ser.acceptJsonFormatVisitor(visitor, _referredType);
 }
コード例 #13
0
 protected JsonSchema propertySchema(BeanProperty prop) throws JsonMappingException {
   if (prop == null) {
     throw new IllegalArgumentException("Null property");
   }
   SchemaFactoryWrapper visitor = wrapperFactory.getWrapper(getProvider());
   JsonSerializer<Object> ser = getSer(prop);
   if (ser != null) {
     JavaType type = prop.getType();
     if (type == null) {
       throw new IllegalStateException("Missing type for property '" + prop.getName() + "'");
     }
     ser.acceptJsonFormatVisitor(visitor, type);
   }
   return visitor.finalSchema();
 }
コード例 #14
0
ファイル: MapSerializer.java プロジェクト: chenhn/joyplus-tv
  protected void serializeTypedFields(
      Map<?, ?> value, JsonGenerator jgen, SerializerProvider provider)
      throws IOException, JsonGenerationException {
    final JsonSerializer<Object> keySerializer = _keySerializer;
    JsonSerializer<Object> prevValueSerializer = null;
    Class<?> prevValueClass = null;
    final HashSet<String> ignored = _ignoredEntries;
    final boolean skipNulls = !provider.isEnabled(SerializationFeature.WRITE_NULL_MAP_VALUES);

    for (Map.Entry<?, ?> entry : value.entrySet()) {
      Object valueElem = entry.getValue();
      // First, serialize key
      Object keyElem = entry.getKey();
      if (keyElem == null) {
        provider.findNullKeySerializer(_keyType, _property).serialize(null, jgen, provider);
      } else {
        // [JACKSON-314] also may need to skip entries with null values
        if (skipNulls && valueElem == null) continue;
        // One twist: is entry ignorable? If so, skip
        if (ignored != null && ignored.contains(keyElem)) continue;
        keySerializer.serialize(keyElem, jgen, provider);
      }

      // And then value
      if (valueElem == null) {
        provider.defaultSerializeNull(jgen);
      } else {
        Class<?> cc = valueElem.getClass();
        JsonSerializer<Object> currSerializer;
        if (cc == prevValueClass) {
          currSerializer = prevValueSerializer;
        } else {
          currSerializer = provider.findValueSerializer(cc, _property);
          prevValueSerializer = currSerializer;
          prevValueClass = cc;
        }
        try {
          currSerializer.serializeWithType(valueElem, jgen, provider, _valueTypeSerializer);
        } catch (Exception e) {
          // [JACKSON-55] Need to add reference information
          String keyDesc = "" + keyElem;
          wrapAndThrow(provider, e, value, keyDesc);
        }
      }
    }
  }
コード例 #15
0
  /**
   * The method to be called by {@link ObjectMapper} and {@link ObjectWriter} for serializing given
   * value, using serializers that this provider has access to (via caching and/or creating new
   * serializers as need be).
   */
  public void serializeValue(JsonGenerator jgen, Object value)
      throws IOException, JsonGenerationException {
    JsonSerializer<Object> ser;
    final boolean wrap;

    if (value == null) { // no type provided; must just use the default null serializer
      ser = getDefaultNullValueSerializer();
      wrap = false; // no name to use for wrapping; can't do!
    } else {
      Class<?> cls = value.getClass();
      // true, since we do want to cache root-level typed serializers (ditto for null property)
      ser = findTypedValueSerializer(cls, true, null);

      // Ok: should we wrap result in an additional property ("root name")?
      String rootName = _config.getRootName();
      if (rootName == null) { // not explicitly specified
        // [JACKSON-163]
        wrap = _config.isEnabled(SerializationFeature.WRAP_ROOT_VALUE);
        if (wrap) {
          jgen.writeStartObject();
          jgen.writeFieldName(_rootNames.findRootName(value.getClass(), _config));
        }
      } else if (rootName.length() == 0) {
        wrap = false;
      } else { // [JACKSON-764]
        // empty String means explicitly disabled; non-empty that it is enabled
        wrap = true;
        jgen.writeStartObject();
        jgen.writeFieldName(rootName);
      }
    }
    try {
      ser.serialize(value, jgen, this);
      if (wrap) {
        jgen.writeEndObject();
      }
    } catch (IOException ioe) { // As per [JACKSON-99], pass IOException and subtypes as-is
      throw ioe;
    } catch (Exception e) { // but wrap RuntimeExceptions, to get path information
      String msg = e.getMessage();
      if (msg == null) {
        msg = "[no message for " + e.getClass().getName() + "]";
      }
      throw new JsonMappingException(msg, e);
    }
  }
  @Override
  public void serializeAsField(Object bean, JsonGenerator jgen, SerializerProvider prov)
      throws Exception {
    Object value = get(bean);
    if (value == null) {
      // Hmmh. I assume we MUST pretty much suppress nulls, since we
      // can't really unwrap them...
      return;
    }
    JsonSerializer<Object> ser = _serializer;
    if (ser == null) {
      Class<?> cls = value.getClass();
      PropertySerializerMap map = _dynamicSerializers;
      ser = map.serializerFor(cls);
      if (ser == null) {
        ser = _findAndAddDynamic(map, cls, prov);
      }
    }
    if (_suppressableValue != null) {
      if (MARKER_FOR_EMPTY == _suppressableValue) {
        if (ser.isEmpty(value)) {
          return;
        }
      } else if (_suppressableValue.equals(value)) {
        return;
      }
    }
    // For non-nulls, first: simple check for direct cycles
    if (value == bean) {
      if (_handleSelfReference(bean, jgen, prov, ser)) {
        return;
      }
    }

    // note: must verify we are using unwrapping serializer; if not, will write field name
    if (!ser.isUnwrappingSerializer()) {
      jgen.writeFieldName(_name);
    }

    if (_typeSerializer == null) {
      ser.serialize(value, jgen, prov);
    } else {
      ser.serializeWithType(value, jgen, prov, _typeSerializer);
    }
  }
コード例 #17
0
 private final void serializeUsingCustom(
     List<String> value, JsonGenerator jgen, SerializerProvider provider, int len)
     throws IOException, JsonGenerationException {
   int i = 0;
   try {
     final JsonSerializer<String> ser = _serializer;
     for (i = 0; i < len; ++i) {
       String str = value.get(i);
       if (str == null) {
         provider.defaultSerializeNull(jgen);
       } else {
         ser.serialize(str, jgen, provider);
       }
     }
   } catch (Exception e) {
     wrapAndThrow(provider, e, value, i);
   }
 }
コード例 #18
0
 @Override
 public boolean isEmpty(SerializerProvider provider, Optional<?> value) {
   if ((value == null) || !value.isPresent()) {
     return true;
   }
   if (_contentInclusion == null) {
     return false;
   }
   Object contents = value.get();
   JsonSerializer<Object> ser = _valueSerializer;
   if (ser == null) {
     try {
       ser = _findCachedSerializer(provider, value.getClass());
     } catch (JsonMappingException e) { // nasty but necessary
       throw new RuntimeJsonMappingException(e);
     }
   }
   return ser.isEmpty(provider, contents);
 }
コード例 #19
0
 public void serializeContentsUsing(
     Object[] paramArrayOfObject,
     JsonGenerator paramJsonGenerator,
     SerializerProvider paramSerializerProvider,
     JsonSerializer<Object> paramJsonSerializer)
     throws IOException {
   int j = paramArrayOfObject.length;
   TypeSerializer localTypeSerializer = this._valueTypeSerializer;
   int i = 0;
   for (; ; ) {
     Object localObject;
     if (i < j) {
       localObject = paramArrayOfObject[i];
       if (localObject == null) {}
       try {
         paramSerializerProvider.defaultSerializeNull(paramJsonGenerator);
       } catch (IOException paramArrayOfObject) {
         throw paramArrayOfObject;
         paramJsonSerializer.serializeWithType(
             localObject, paramJsonGenerator, paramSerializerProvider, localTypeSerializer);
       } catch (Exception paramArrayOfObject) {
         while (((paramArrayOfObject instanceof InvocationTargetException))
             && (paramArrayOfObject.getCause() != null)) {
           paramArrayOfObject = paramArrayOfObject.getCause();
         }
         if (!(paramArrayOfObject instanceof Error)) {
           break label108;
         }
         throw ((Error) paramArrayOfObject);
         throw JsonMappingException.wrapWithPath(paramArrayOfObject, localObject, i);
       }
       if (localTypeSerializer == null) {
         paramJsonSerializer.serialize(localObject, paramJsonGenerator, paramSerializerProvider);
         break label118;
       }
     }
     label108:
     return;
     label118:
     i += 1;
   }
 }
コード例 #20
0
 private void serializeContentsSlow(
     String[] value, JsonGenerator jgen, SerializerProvider provider, JsonSerializer<Object> ser)
     throws IOException, JsonGenerationException {
   int len = value.length;
   for (int i = 0; i < len; i++) {
     if (value[i] == null) {
       provider.defaultSerializeNull(jgen);
     } else {
       ser.serialize(value[i], jgen, provider);
     }
   }
 }
コード例 #21
0
 @Override
 public void serialize(Optional<?> opt, JsonGenerator gen, SerializerProvider provider)
     throws IOException {
   if (!opt.isPresent()) {
     // 22-Oct-2015, tatu: With unwrapping we can not serialize value, just key/value pairs so:
     if (_unwrapper == null) {
       provider.defaultSerializeNull(gen);
     }
     return;
   }
   Object value = opt.get();
   JsonSerializer<Object> ser = _valueSerializer;
   if (ser == null) {
     ser = _findCachedSerializer(provider, value.getClass());
   }
   if (_valueTypeSerializer != null) {
     ser.serializeWithType(value, gen, provider, _valueTypeSerializer);
   } else {
     ser.serialize(value, gen, provider);
   }
 }
コード例 #22
0
  /**
   * General-purpose serialization for contents, where we do not necessarily know the value
   * serialization, but we do know that no value suppression is needed (which simplifies processing
   * a bit)
   */
  public void serializeFields(Map<?, ?> value, JsonGenerator gen, SerializerProvider provider)
      throws IOException {
    // If value type needs polymorphic type handling, some more work needed:
    if (_valueTypeSerializer != null) {
      serializeTypedFields(value, gen, provider, null);
      return;
    }
    final JsonSerializer<Object> keySerializer = _keySerializer;
    final Set<String> ignored = _ignoredEntries;
    Object keyElem = null;

    try {
      for (Map.Entry<?, ?> entry : value.entrySet()) {
        Object valueElem = entry.getValue();
        // First, serialize key
        keyElem = entry.getKey();
        if (keyElem == null) {
          provider.findNullKeySerializer(_keyType, _property).serialize(null, gen, provider);
        } else {
          // One twist: is entry ignorable? If so, skip
          if ((ignored != null) && ignored.contains(keyElem)) {
            continue;
          }
          keySerializer.serialize(keyElem, gen, provider);
        }
        // And then value
        if (valueElem == null) {
          provider.defaultSerializeNull(gen);
          continue;
        }
        JsonSerializer<Object> serializer = _valueSerializer;
        if (serializer == null) {
          serializer = _findSerializer(provider, valueElem);
        }
        serializer.serialize(valueElem, gen, provider);
      }
    } catch (Exception e) { // Add reference information
      wrapAndThrow(provider, e, value, String.valueOf(keyElem));
    }
  }
  // need to override as we must get unwrapping instance...
  @Override
  protected JsonSerializer<Object> _findAndAddDynamic(
      PropertySerializerMap map, Class<?> type, SerializerProvider provider)
      throws JsonMappingException {
    JsonSerializer<Object> serializer;
    if (_nonTrivialBaseType != null) {
      JavaType subtype = provider.constructSpecializedType(_nonTrivialBaseType, type);
      serializer = provider.findValueSerializer(subtype, this);
    } else {
      serializer = provider.findValueSerializer(type, this);
    }
    NameTransformer t = _nameTransformer;
    if (serializer.isUnwrappingSerializer()) {
      t =
          NameTransformer.chainedTransformer(
              t, ((UnwrappingBeanSerializer) serializer)._nameTransformer);
    }
    serializer = serializer.unwrappingSerializer(t);

    _dynamicSerializers = _dynamicSerializers.newWith(type, serializer);
    return serializer;
  }
コード例 #24
0
  /**
   * The method to be called by {@link ObjectMapper} and {@link ObjectWriter} for serializing given
   * value (assumed to be of specified root type, instead of runtime type of value), using
   * serializers that this provider has access to (via caching and/or creating new serializers as
   * need be),
   *
   * @param rootType Type to use for locating serializer to use, instead of actual runtime type.
   *     Must be actual type, or one of its super types
   */
  public void serializeValue(JsonGenerator jgen, Object value, JavaType rootType)
      throws IOException, JsonGenerationException {
    final boolean wrap;

    JsonSerializer<Object> ser;
    if (value == null) {
      ser = getDefaultNullValueSerializer();
      wrap = false;
    } else {
      // Let's ensure types are compatible at this point
      if (!rootType.getRawClass().isAssignableFrom(value.getClass())) {
        _reportIncompatibleRootType(value, rootType);
      }
      // root value, not reached via property:
      ser = findTypedValueSerializer(rootType, true, null);
      // [JACKSON-163]
      wrap = _config.isEnabled(SerializationFeature.WRAP_ROOT_VALUE);
      if (wrap) {
        jgen.writeStartObject();
        jgen.writeFieldName(_rootNames.findRootName(rootType, _config));
      }
    }
    try {
      ser.serialize(value, jgen, this);
      if (wrap) {
        jgen.writeEndObject();
      }
    } catch (IOException ioe) { // no wrapping for IO (and derived)
      throw ioe;
    } catch (Exception e) { // but others do need to be, to get path etc
      String msg = e.getMessage();
      if (msg == null) {
        msg = "[no message for " + e.getClass().getName() + "]";
      }
      throw new JsonMappingException(msg, e);
    }
  }
コード例 #25
0
ファイル: MapSerializer.java プロジェクト: chenhn/joyplus-tv
  /**
   * Method called to serialize fields, when the value type is statically known, so that value
   * serializer is passed and does not need to be fetched from provider.
   */
  protected void serializeFieldsUsing(
      Map<?, ?> value, JsonGenerator jgen, SerializerProvider provider, JsonSerializer<Object> ser)
      throws IOException, JsonGenerationException {
    final JsonSerializer<Object> keySerializer = _keySerializer;
    final HashSet<String> ignored = _ignoredEntries;
    final TypeSerializer typeSer = _valueTypeSerializer;
    final boolean skipNulls = !provider.isEnabled(SerializationFeature.WRITE_NULL_MAP_VALUES);

    for (Map.Entry<?, ?> entry : value.entrySet()) {
      Object valueElem = entry.getValue();
      Object keyElem = entry.getKey();
      if (keyElem == null) {
        provider.findNullKeySerializer(_keyType, _property).serialize(null, jgen, provider);
      } else {
        // [JACKSON-314] also may need to skip entries with null values
        if (skipNulls && valueElem == null) continue;
        if (ignored != null && ignored.contains(keyElem)) continue;
        keySerializer.serialize(keyElem, jgen, provider);
      }
      if (valueElem == null) {
        provider.defaultSerializeNull(jgen);
      } else {
        try {
          if (typeSer == null) {
            ser.serialize(valueElem, jgen, provider);
          } else {
            ser.serializeWithType(valueElem, jgen, provider, typeSer);
          }
        } catch (Exception e) {
          // [JACKSON-55] Need to add reference information
          String keyDesc = "" + keyElem;
          wrapAndThrow(provider, e, value, keyDesc);
        }
      }
    }
  }
コード例 #26
0
 protected void serializeContents(
     EnumMap<? extends Enum<?>, ?> paramEnumMap,
     JsonGenerator paramJsonGenerator,
     SerializerProvider paramSerializerProvider)
     throws IOException, JsonGenerationException {
   if (this._valueSerializer != null) {
     serializeContentsUsing(
         paramEnumMap, paramJsonGenerator, paramSerializerProvider, this._valueSerializer);
   }
   label70:
   label207:
   label291:
   label303:
   for (; ; ) {
     return;
     Object localObject1 = null;
     Object localObject4 = null;
     Object localObject2 = this._keyEnums;
     int i;
     boolean bool;
     TypeSerializer localTypeSerializer;
     Iterator localIterator;
     if (!paramSerializerProvider.isEnabled(SerializationFeature.WRITE_NULL_MAP_VALUES)) {
       i = 1;
       bool = paramSerializerProvider.isEnabled(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
       localTypeSerializer = this._valueTypeSerializer;
       localIterator = paramEnumMap.entrySet().iterator();
     }
     for (; ; ) {
       if (!localIterator.hasNext()) {
         break label303;
       }
       Map.Entry localEntry = (Map.Entry) localIterator.next();
       Object localObject6 = localEntry.getValue();
       if ((i == 0) || (localObject6 != null)) {
         Object localObject5 = (Enum) localEntry.getKey();
         if (bool) {
           paramJsonGenerator.writeFieldName(((Enum) localObject5).toString());
         }
         Object localObject3;
         for (; ; ) {
           if (localObject6 != null) {
             break label207;
           }
           paramSerializerProvider.defaultSerializeNull(paramJsonGenerator);
           break label70;
           i = 0;
           break;
           localObject3 = localObject2;
           if (localObject2 == null) {
             localObject3 =
                 ((EnumSerializer)
                         paramSerializerProvider.findValueSerializer(
                             ((Enum) localObject5).getDeclaringClass(), this._property))
                     .getEnumValues();
           }
           paramJsonGenerator.writeFieldName(
               ((EnumValues) localObject3).serializedValueFor((Enum) localObject5));
           localObject2 = localObject3;
         }
         localObject5 = localObject6.getClass();
         if (localObject5 == localObject4) {
           localObject3 = localObject1;
         }
         JsonSerializer localJsonSerializer;
         for (; ; ) {
           if (localTypeSerializer != null) {
             break label291;
           }
           try {
             ((JsonSerializer) localObject3)
                 .serialize(localObject6, paramJsonGenerator, paramSerializerProvider);
           } catch (Exception localException) {
             wrapAndThrow(
                 paramSerializerProvider,
                 localException,
                 paramEnumMap,
                 ((Enum) localEntry.getKey()).name());
           }
           break;
           localJsonSerializer =
               paramSerializerProvider.findValueSerializer((Class) localObject5, this._property);
           localObject1 = localJsonSerializer;
           localObject4 = localObject5;
         }
         localJsonSerializer.serializeWithType(
             localObject6, paramJsonGenerator, paramSerializerProvider, localTypeSerializer);
       }
     }
   }
 }
コード例 #27
0
 protected void serializeContentsUsing(
     EnumMap<? extends Enum<?>, ?> paramEnumMap,
     JsonGenerator paramJsonGenerator,
     SerializerProvider paramSerializerProvider,
     JsonSerializer<Object> paramJsonSerializer)
     throws IOException, JsonGenerationException {
   Object localObject1 = this._keyEnums;
   int i;
   boolean bool;
   TypeSerializer localTypeSerializer;
   Iterator localIterator;
   if (!paramSerializerProvider.isEnabled(SerializationFeature.WRITE_NULL_MAP_VALUES)) {
     i = 1;
     bool = paramSerializerProvider.isEnabled(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
     localTypeSerializer = this._valueTypeSerializer;
     localIterator = paramEnumMap.entrySet().iterator();
   }
   for (; ; ) {
     label45:
     if (!localIterator.hasNext()) {
       return;
     }
     Map.Entry localEntry = (Map.Entry) localIterator.next();
     Object localObject3 = localEntry.getValue();
     if ((i == 0) || (localObject3 != null)) {
       Enum localEnum = (Enum) localEntry.getKey();
       if (bool) {
         paramJsonGenerator.writeFieldName(localEnum.toString());
       }
       for (; ; ) {
         if (localObject3 != null) {
           break label182;
         }
         paramSerializerProvider.defaultSerializeNull(paramJsonGenerator);
         break label45;
         i = 0;
         break;
         Object localObject2 = localObject1;
         if (localObject1 == null) {
           localObject2 =
               ((EnumSerializer)
                       paramSerializerProvider.findValueSerializer(
                           localEnum.getDeclaringClass(), this._property))
                   .getEnumValues();
         }
         paramJsonGenerator.writeFieldName(
             ((EnumValues) localObject2).serializedValueFor(localEnum));
         localObject1 = localObject2;
       }
       label182:
       if (localTypeSerializer == null) {
         try {
           paramJsonSerializer.serialize(
               localObject3, paramJsonGenerator, paramSerializerProvider);
         } catch (Exception localException) {
           wrapAndThrow(
               paramSerializerProvider,
               localException,
               paramEnumMap,
               ((Enum) localEntry.getKey()).name());
         }
       } else {
         paramJsonSerializer.serializeWithType(
             localObject3, paramJsonGenerator, paramSerializerProvider, localTypeSerializer);
       }
     }
   }
 }