|
| 1 | +package com.genexus.json; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +import java.util.Map.Entry; |
| 6 | +import org.json.JSONException; |
| 7 | +import org.json.JSONObject; |
| 8 | +public class JSONObjectWrapper extends JSONObject{ |
| 9 | + private Map<String, Object> map; |
| 10 | + |
| 11 | + public JSONObjectWrapper() { |
| 12 | + super(); |
| 13 | + if (map == null) |
| 14 | + map = new LinkedHashMap<String, Object>(); |
| 15 | + } |
| 16 | + |
| 17 | + public JSONObjectWrapper(String string) { |
| 18 | + super(string); |
| 19 | + if (map == null) |
| 20 | + map = new LinkedHashMap<String, Object>(); |
| 21 | + } |
| 22 | + |
| 23 | + public JSONObjectWrapper(Map<?,?> m) { |
| 24 | + super(m); |
| 25 | + if (map == null) |
| 26 | + map = new LinkedHashMap<String, Object>(); |
| 27 | + else { |
| 28 | + map = new LinkedHashMap<String, Object>(m.size()); |
| 29 | + for (final Entry<?, ?> e : m.entrySet()) { |
| 30 | + if (e.getKey() == null) { |
| 31 | + throw new NullPointerException("Null key."); |
| 32 | + } |
| 33 | + final Object value = e.getValue(); |
| 34 | + if (value != null) |
| 35 | + this.map.put(String.valueOf(e.getKey()), value); |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + public JSONObjectWrapper(JSONObject jsonObject) { |
| 41 | + super(jsonObject.toString()); |
| 42 | + if (map == null) |
| 43 | + map = new LinkedHashMap<String, Object>(); |
| 44 | + } |
| 45 | + |
| 46 | + public Set<Entry<String, Object>> entrySet() { |
| 47 | + return map.entrySet(); |
| 48 | + } |
| 49 | + |
| 50 | + public void clear() { |
| 51 | + super.clear(); |
| 52 | + map.clear(); |
| 53 | + } |
| 54 | + |
| 55 | + public JSONObjectWrapper put(String key, Object value) throws JSONException { |
| 56 | + if (value == null) |
| 57 | + super.put(key, JSONObject.NULL); |
| 58 | + else |
| 59 | + super.put(key, value); |
| 60 | + if (map == null) |
| 61 | + map = new LinkedHashMap<String, Object>(); |
| 62 | + if (!map.containsKey(key)) |
| 63 | + map.put(key, value); |
| 64 | + return this; |
| 65 | + } |
| 66 | + |
| 67 | + public Object remove(String key) { |
| 68 | + if (map.containsKey(key)) |
| 69 | + map.remove(key); |
| 70 | + return super.remove(key); |
| 71 | + } |
| 72 | +} |
0 commit comments