杰克逊 - 将地图实现反序列化为HashMap

时间:2016-12-09 15:02:35

标签: java json serialization jackson deserialization

所以,我有配置

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AimeosUsersTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function(Blueprint $table)
        {
            $table->string('salutation', 8)->default('');
            $table->string('company', 100)->default('');
            $table->string('vatid', 32)->default('');
            $table->string('title', 64)->default('');
            $table->string('firstname', 64)->default('');
            $table->string('lastname', 64)->default('');
            $table->string('address1')->default('');
            $table->string('address2')->default('');
            $table->string('address3')->default('');
            $table->string('postal', 16)->default('');
            $table->string('city')->default('');
            $table->string('state')->default('');
            $table->string('langid', 5)->nullable();
            $table->char('countryid', 2)->nullable();
            $table->string('telephone', 32)->default('');
            $table->string('telefax', 32)->default('');
            $table->string('website')->default('');
            $table->date('birthday')->nullable();
            $table->smallInteger('status')->default('1');
            $table->date('vdate')->nullable();
            $table->string('editor')->default('');

            $table->index('langid');
            $table->index(array('status', 'lastname', 'firstname'));
            $table->index(array('status', 'address1'));
            $table->index('lastname');
            $table->index('address1');
            $table->index('city');
            $table->index('postal');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('users', function(Blueprint $table)
        {
            $table->dropIndex('users_langid_index');
            $table->dropIndex('users_status_lastname_firstname_index');
            $table->dropIndex('users_status_address1_index');
            $table->dropIndex('users_lastname_index');
            $table->dropIndex('users_address1_index');
            $table->dropIndex('users_city_index');
            $table->dropIndex('users_postal_index');

            $table->dropColumn(array(
                'salutation', 'company', 'vatid', 'title', 'firstname',
                'lastname', 'address1', 'address2', 'address3', 'postal', 'city',
                'state', 'langid', 'countryid', 'telephone', 'telefax', 'website',
                'birthday', 'status', 'vdate', 'editor'
            ));
        });
    }

}

正确编写序列化的json为:

.defaultTyping(NON_FINAL)

现在,问题是MessageHeaders类正在实现Map,但也会覆盖put方法,因此会引发异常。为了正确反序列化,我需要能够将其序列化为HashMap,所以:

"headers": [
        "org.springframework.messaging.MessageHeaders",
        {

或者能够将MessageHeaders显式反序列化为HashMap(因为它实际上只是Map。

再一个问题是:如何序列化实现Map为HashMap的Object,或者如何让Object实现Map反序列化为HashMap。

1 个答案:

答案 0 :(得分:1)

好的,我发现如何使用自定义序列化程序(诀窍是告诉序列化程序使用java.util.HashMap而不是真正的类名):

public class MessageHeadersJsonSerializer extends JsonSerializer<MessageHeaders>{
    @Override
    public void serializeWithType(MessageHeaders value, JsonGenerator gen, SerializerProvider serializers, TypeSerializer typeSer) throws IOException {
        typeSer.writeTypePrefixForObject(value, gen, HashMap.class);
        serialize(value, gen, serializers);
        typeSer.writeTypeSuffixForObject(value, gen);
    }

    @Override
    public void serialize(MessageHeaders value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        for(Map.Entry<String, Object> entry : value.entrySet()){
            gen.writeFieldName(entry.getKey());
            gen.writeObject(entry.getValue());
        }
    }
}

然后将其注册为MixIn:

@JsonSerialize(using = MessageHeadersJsonSerializer.class)
public abstract class MessageHeadersMixIn {}

在父对象上我将其反序列化为HashMap:

public abstract class GenericMessageMixIn<T> {
    @JsonCreator
    public GenericMessageMixIn(
            @JsonProperty("payload") T payload,
            @JsonProperty("headers") Map<String, Object> headers
    ){}
}

最后一切正常!