Home | History | Annotate | Download | only in json
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.motorola.studio.android.json;
     18 
     19 import java.util.ArrayList;
     20 import java.util.HashSet;
     21 import java.util.Iterator;
     22 import java.util.List;
     23 import java.util.Set;
     24 
     25 /**
     26  * This class is responsible to parse a JSON string into objects
     27  * Visit {@link http://www.json.org/} for more information
     28  *
     29  */
     30 public class Jason
     31 {
     32     private final Set<JSONObject> objects;
     33 
     34     public Jason(String value)
     35     {
     36         objects = new HashSet<JSONObject>();
     37         stip(value);
     38     }
     39 
     40     public Set<JSONObject> getJSON()
     41     {
     42         return objects;
     43     }
     44 
     45     private void stip(String value)
     46     {
     47         List<Character> json = new ArrayList<Character>();
     48         for (char c : value.toCharArray())
     49         {
     50             json.add(c);
     51         }
     52 
     53         while (json.size() > 0)
     54         {
     55             Character next = json.get(0);
     56 
     57             if (next == '{')
     58             {
     59                 JSONObject object = (JSONObject) JSONObject.parse(json);
     60                 objects.add(object);
     61             }
     62             else if ((next == ' ') || (next == '\r') || (next == '\n') || (next == ','))
     63             {
     64                 json.remove(0);
     65             }
     66         }
     67     }
     68 
     69     @Override
     70     public String toString()
     71     {
     72         String string = "";
     73         Iterator<JSONObject> objectIterator = objects.iterator();
     74         while (objectIterator.hasNext())
     75         {
     76             string += objectIterator.next().toString();
     77             if (objectIterator.hasNext())
     78             {
     79                 string += ",";
     80             }
     81         }
     82         return string;
     83     }
     84 }
     85