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 /** 037 * Keeps an ordered copy of the JSON object members. 038 */ 039 private final Map<String,Object> orderedMap = new LinkedHashMap<>(); 040 041 042 @Override 043 public Object put(final String s, final Object o) { 044 orderedMap.put(s, o); 045 return super.put(s, o); 046 } 047 048 049 @Override 050 public void putAll(Map<? extends String, ?> map) { 051 orderedMap.putAll(map); 052 super.putAll(map); 053 } 054 055 056 @Override 057 public Set<String> keySet() { 058 return orderedMap.keySet(); 059 } 060 061 062 @Override 063 public Set<Entry<String, Object>> entrySet() { 064 return orderedMap.entrySet(); 065 } 066 067 068 @Override 069 public Object remove(Object o) { 070 orderedMap.remove(o); 071 return super.remove(o); 072 } 073 074 075 @Override 076 public void clear() { 077 orderedMap.clear(); 078 super.clear(); 079 } 080 081 082 @Override 083 public String toJSONString() { 084 return JSONObject.toJSONString(orderedMap); 085 } 086}