aboutsummaryrefslogtreecommitdiff
path: root/src/uk/org/ury/server/protocol/ProtocolUtils.java
blob: e45bb22a24ad56642356bc274bfa884e9cc621e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
 * 
 */
package uk.org.ury.server.protocol;

import java.util.Map;

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;


/**
 * Utilities for converting between strings encoded in the response 
 * protocol and collections of items.
 * 
 * @author  Matt Windsor
 *
 */

public class ProtocolUtils
{
  /**
   * Encode a key-value map into a protocol string.
   * 
   * The map can contain strings, lists and other maps.  Other 
   * types may be accepted by the underlying encoding engine, 
   * but the above types are the only ones explicitly accepted.
   * 
   * @param items  The key-value map of items, which should contain 
   *               strings, lists and maps.  The keys of any map 
   *               should be protocol directives.
   *               
   * @return       A string containing the encoded representation of 
   *               the map.
   */
  
  public static String
  encode (Map<String, Object> items)
  {
    return JSONValue.toJSONString (items);
  }
  
  
  /**
   * Decode a protocol string into a key-value map.
   * 
   * @param string  The string to decode.
   * 
   * @return        A key-value map mapping directives to strings,
   *                lists and maps.
   *                
   * @throws        DecodeFailureException if the decoding engine 
   *                returns something other than a map.
   */
  
  public static Map<?, ?>
  decode (String string) throws DecodeFailureException
  {
    Object result = JSONValue.parse (string);
    
    if (result instanceof JSONObject)
      return (JSONObject) result;
    else
      throw new DecodeFailureException ("Result not a map.");
  }
  
}