Jackson Json Serializer附加内部属性

时间:2017-01-16 07:30:04

标签: java jackson apple-push-notifications

我正在为Apple Push Notification Service解析类到JSON。所以我有一个acme类,并希望在最终的JSON中附加额外的,可选的和动态的{ "aps": { "alert": { "body": "body", "title": "title" }, "badge": 123, "category": "category" }, "acme": { "1": "1", "value1": "value1" } } 属性。这意味着我想转换下面的Jackson输出...

{
    "aps": {
        "alert": {
            "body": "body",
            "title": "title"
        },
        "badge": 123,
        "category": "category"
    },
    "acme-1": "1",
    "acme-value1": "value1"
}

对此...

acme-

我已成功为@JsonNaming()的密钥添加前缀@Value @Builder @JsonInclude(JsonInclude.Include.NON_NULL) // need a serializer to move up properties in Acme by one level // @JsonSerialize(using = NewSerializer.class) public class ApnsPayload { private Aps aps; private Acme acme; @JsonNaming(AcmeNamingStrategy.class) // add prefix acme- public interface Acme { } @Value @Builder @JsonInclude(JsonInclude.Include.NON_NULL) public static class Aps { private Alert alert; private Integer badge; private String sound; @JsonProperty("content-available") private Integer contentAvailable; private String category; @JsonProperty("thread-id") private String threadId; @Value @Builder @JsonInclude(JsonInclude.Include.NON_NULL) public static class Alert { private String title; private String body; @JsonProperty("title-loc-key") private String titleLocalizationKey; @JsonProperty("title-loc-args") private List<String> titleLocalizationArgs; @JsonProperty("action-loc-key") private String actionLocalizationKey; @JsonProperty("loc-key") private String localizationKey; @JsonProperty("loc-args") private List<String> localizationArgs; @JsonProperty("launch-image") private String launchImage; } } } ,但我无法将这些属性移动一级。请帮忙,谢谢!

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setReverseLayout(true);
        linearLayoutManager.setStackFromEnd(true);
        mRecyclerView.setLayoutManager(linearLayoutManager);

        if (savedInstanceState != null) {
            // Restore saved layout manager type.
            mRecyclerViewPosition = (int) savedInstanceState
                    .getSerializable(KEY_LAYOUT_POSITION);
            mRecyclerView.scrollToPosition(mRecyclerViewPosition);
            // TODO: RecyclerView only restores position properly for some tabs.
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        buildGoogleApiClient();
        buildLocationSettingsRequest();
        checkLocationSettings();
        createLocationRequest();
        mFirebaseRef = FirebaseDatabase.getInstance().getReference(POSTS_STRING);
        mFirebaseRef.keepSynced(true);
        this.geoFire = new GeoFire(FirebaseDatabase.getInstance().getReference(GEO_POINTS));
        mAdapter = new FirebasePostsQueryAdapter(mFirebaseRef.equalTo(GEO_POINTS), getActivity());

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.geo_fragment_layout, container, false);
        mRecyclerView = (RecyclerView) rootView.findViewById(R.id.geo_recycler_view);

        return rootView;
    }

    private void bindRecyclerView() {
        mRecyclerView.setAdapter(mAdapter);
    }

    private void startGeoQuery() {
        query = geoFire.queryAtLocation(center, 2);
        Log.d(TAG, "center: " + center.toString() + ", radius: " + 2);
        query.addGeoQueryEventListener(new GeoQueryEventListener() {
            @Override
            public void onKeyEntered(String key, GeoLocation location) {
                Log.d(TAG, "Key " + key + " entered the search area at [" + location.latitude + "," + location.longitude + "]");
                final DatabaseReference tempRef = mFirebaseRef.child(key);
                tempRef.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot snapshot) {
                        // I  add the deal only if it doesn't exist already in the adapter
                        String key = snapshot.getKey();
                        if (!mAdapter.exists(key)) {
                            Log.d(TAG, "item added " + key);
                            mAdapter.addSingle(snapshot);
                            mAdapter.notifyDataSetChanged();
                            mListContainer.setViewState(MultiStateView.VIEW_STATE_CONTENT);
                        } else {
                            //...otherwise I will update the record
                            Log.d(TAG, "item updated: " + key);
                            mAdapter.update(snapshot, key);
                            mAdapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
            }

            @Override
            public void onKeyExited(String key) {
                Log.d(TAG, "deal " + key + " is no longer in the search area");
                mAdapter.remove(key);
                isListEmpty();

            }

            @Override
            public void onKeyMoved(String key, GeoLocation location) {
                Log.d(TAG, String.format("Key " + key + " moved within the search area to [%f,%f]", location.latitude, location.longitude));
                final DatabaseReference tempRef = mFirebaseRef.child(key);
                tempRef.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot snapshot) {
                        // I  add the deal only if it doesn't exist already in the adapter
                        String key = snapshot.getKey();
                        if (!mAdapter.exists(key)) {
                            Log.d(TAG, "item added " + key);
                            mAdapter.addSingle(snapshot);
                            mAdapter.notifyDataSetChanged();
                            mListContainer.setViewState(MultiStateView.VIEW_STATE_CONTENT);
                        } else {
                            //...otherwise I will update the record
                            Log.d(TAG, "item updated: " + key);
                            mAdapter.update(snapshot, key);
                            mAdapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
            }

            @Override
            public void onGeoQueryReady() {
                Log.d(TAG, "All initial data has been loaded and events have been fired!");
                isListEmpty();
                mActiveGeoQuery = true;
            }

            @Override
            public void onGeoQueryError(DatabaseError error) {
                Log.e(TAG, "There was an error with this query: " + error);
                mListContainer.setViewState(MultiStateView.VIEW_STATE_ERROR);
                mActiveGeoQuery = false;
            }
        });
        mRecyclerView.setAdapter(mAdapter);
    }

1 个答案:

答案 0 :(得分:1)

LOL,我在发布此问题后2分钟找到答案。我需要的只是Jackson Unwrapping Feature,方法是将@JsonUnwrapped添加到要解包的属性中。

@JsonUnwrapped
private Acme acme;
相关问题