001/*
002 * oauth2-oidc-sdk
003 *
004 * Copyright 2012-2016, Connect2id Ltd and contributors.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
007 * this file except in compliance with the License. You may obtain a copy of the
008 * License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software distributed
013 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
014 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
015 * specific language governing permissions and limitations under the License.
016 */
017
018package com.nimbusds.oauth2.sdk.util;
019
020
021import java.util.LinkedHashMap;
022import java.util.Map;
023import java.util.Set;
024
025import net.jcip.annotations.NotThreadSafe;
026import net.minidev.json.JSONObject;
027
028
029/**
030 * Ordered JSON object.
031 */
032@NotThreadSafe
033public class OrderedJSONObject extends JSONObject {
034        
035        
036        private static final long serialVersionUID = -8682025379611131137L;
037        
038        
039        /**
040         * Keeps an ordered copy of the JSON object members.
041         */
042        private final Map<String,Object> orderedMap = new LinkedHashMap<>();
043        
044        
045        @Override
046        public Object put(final String s, final Object o) {
047                orderedMap.put(s, o);
048                return super.put(s, o);
049        }
050        
051        
052        @Override
053        public void putAll(Map<? extends String, ?> map) {
054                orderedMap.putAll(map);
055                super.putAll(map);
056        }
057        
058        
059        @Override
060        public Set<String> keySet() {
061                return orderedMap.keySet();
062        }
063        
064        
065        @Override
066        public Set<Entry<String, Object>> entrySet() {
067                return orderedMap.entrySet();
068        }
069        
070        
071        @Override
072        public Object remove(Object o) {
073                orderedMap.remove(o);
074                return super.remove(o);
075        }
076        
077        
078        @Override
079        public void clear() {
080                orderedMap.clear();
081                super.clear();
082        }
083        
084        
085        @Override
086        public String toJSONString() {
087                return JSONObject.toJSONString(orderedMap);
088        }
089}