开发者

How to transform SimpleOrderedMap into JSON string or JSON object?

Is there any standard way to do 开发者_JAVA技巧so?


In short: no, because JSON doesn't have a primitive ordered map type.

The first step is to determine the requirements of your client, as far as decoding the JSON string goes. Since the JSON specification doesn't have an ordered map type, you'll have to decide on a representation to use. The choice you make will depend on the decoding requirements of your client.

If you have full control over the decoding of the JSON string, you can simply encode the object into a map in order, using a JSON library that is guaranteed to serialize things in the order of an iterator that you pass in.

If you can't guarantee this, you should come up with a representation on your own. Two simple examples are:

An alternating list:

"[key1, value1, key2, value2]"

A list of key/value entry objects:

"[{key: key1, val:value1}, {key: key2, val:value2}]"

Once you've come up with this representation, it's easy to write a simple function that loops over your SimpleOrderedMap. For example :

JSONArray jarray = new JSONArray();
for(Map.Entry e : simpleOrderedMap) {
    jarray.put(e.key());
    jarray.put(e.value());
}


Java version:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.solr.common.util.NamedList;

public class SolrMapConverter {

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static Object toMap(Object entry) {
        Object response = null;
        if (entry instanceof NamedList) {
            response = new HashMap<>();
            NamedList lst = (NamedList) entry;
            for (int i = 0; i < lst.size(); i++) {
                ((Map) response).put(lst.getName(i), toMap(lst.getVal(i)));
            }
        } else if (entry instanceof Iterable) {
            response = new ArrayList<>();
            for (Object e : (Iterable) entry) {
                ((ArrayList<Object>) response).add(toMap(e));
            }
        } else if (entry instanceof Map) {
            response = new HashMap<>();
            for (Entry<String, ?> e : ((Map<String, ?>) entry).entrySet()) {
                ((Map) response).put(e.getKey(), toMap(e.getValue()));
            }
        } else {
            return entry;
        }
        return response;
    }
}


Simple adding fields to map won't work, as complex objects (which does not serialize to JSON) could be there.

Here's a groovy code that does it.

 protected static toMap(entry){
  def response
  if(entry instanceof SolrDocumentList){
      def docs = []
      response = [numFound:entry.numFound, maxScore:entry.maxScore, start:entry.start, docs:docs]
      entry.each {
          docs << toMap(it)
      }
  } else
  if(entry instanceof List){
      response = []
      entry.each {
          response << toMap(it)
      }
  } else
  if(entry instanceof Iterable){
      response = [:]
      entry.each {
          if(it instanceof Map.Entry)
    response.put(it.key, toMap(it.value))
          else
            response.put(entry.hashCode(), toMap(it))
      }
  } else
 if (entry instanceof Map){
      response = [:]
      entry.each {
          if(it instanceof Map.Entry)
    response.put(it.key, toMap(it.value))
          else
            response.put(entry.hashCode(), toMap(it))
      }
  } else  {
      response = entry
  }
  return response
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜