Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
License Agreement. JBoss RichFaces - Ajax4jsf Component Library Copyright (C) 2007 Exadel, Inc. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
 package org.richfaces.json;
 
 import java.util.Set;

Author(s):
Nick Belaevski - nbelaevski@exadel.com created 15.12.2006
 
 public class JSONMap extends AbstractMap implements Serializable {
     private static final long serialVersionUID = 2898468948832273123L;
     private JSONObject jsonObject;
 
     public JSONMap() throws JSONException {
         super();
         this. = new JSONObject();
     }
 
     public JSONMap(JSONObject object) {
         super();
         this. = object;
     }
 
     public JSONMap(String jsonStringthrows JSONException {
         super();
         this. = new JSONObject(jsonString);
     }
 
     @Override
     public Set entrySet() {
         return new MyAbstractSet();
     }
 
     @Override
     public Object put(Object keyObject value) {
         String keyString = key.toString();
 
         try {
             Object previousValue = JSONAccessor.getValue(keyString);
 
             JSONAccessor.putValue(keyStringvalue);
 
             return previousValue;
         } catch (JSONException e) {
             throw new RuntimeException(e.getMessage(), e);
         }
     }
 
     public String getString() throws JSONException {
         return .toString(0);
     }
 
     public String getString(int indentFactorthrows JSONException {
         return .toString(indentFactor);
     }
 
     private class MyAbstractSet extends AbstractSet {
         @Override
         public Iterator iterator() {
             return new MyIterator();
         }
 
         @Override
         public int size() {
             return .length();
         }
 
         @Override
         public boolean add(Object o) {
             Entry entry = (Entryo;
 
             return JSONAccessor.putValue(, (Stringentry.getKey(), entry.getValue());
         }
 
         private class MyIterator implements Iterator {
             private Iterator keys = .keys();
             private String currentName;
            public boolean hasNext() {
                return .hasNext();
            }
            public Object next() {
                 = (String.next();
                return new Entry() {
                    private String key = ;
                    public Object getKey() {
                        return ;
                    }
                    public Object getValue() {
                        try {
                            return JSONAccessor.getValue(this.);
                        } catch (JSONException e) {
                            throw new RuntimeException(e.getMessage(), e);
                        }
                    }
                    public Object setValue(Object value) {
                        throw new UnsupportedOperationException();
                    }
                };
            }
            public void remove() {
                throw new UnsupportedOperationException();
            }
        }
    }
New to GrepCode? Check out our FAQ X