public
Description: Java port of the json gem, for JRuby
Homepage: http://json-jruby.rubyforge.org/
Clone URL: git://github.com/mernen/json-jruby.git
Some minor code tweaks.
mernen (author)
Mon Jul 21 23:52:27 -0700 2008
commit  fec7cd29cb3d661e01bf2d4187a27cfa7baba049
tree    6355a221c6597baa874858933aaaecf11db9b3bf
parent  f64a3432fbd033c331f92268d911b83bca344eb3
...
96
97
98
99
100
 
 
 
101
102
103
...
112
113
114
115
 
116
117
118
...
126
127
128
129
130
131
 
132
133
134
...
136
137
138
 
 
 
139
140
141
142
 
143
144
145
...
150
151
152
153
154
155
 
 
156
157
158
...
96
97
98
 
 
99
100
101
102
103
104
...
113
114
115
 
116
117
118
119
...
127
128
129
 
 
 
130
131
132
133
...
135
136
137
138
139
140
141
142
143
144
145
146
147
148
...
153
154
155
 
 
 
156
157
158
159
160
0
@@ -96,8 +96,9 @@ class GeneratorMethodsLoader {
0
          * @return The JSON representation of the Hash
0
          */
0
         private RubyString simpleTransform(RubyHash self) {
0
- int preSize = 2 + Math.max(self.size() * 8, 0);
0
- final RubyString result = self.getRuntime().newString(new ByteList(preSize));
0
+ final int preSize = 2 + Math.max(self.size() * 12, 0);
0
+ final RubyString result =
0
+ self.getRuntime().newString(new ByteList(preSize));
0
             result.cat((byte)'{');
0
             self.visitAll(new RubyHash.Visitor() {
0
                 private boolean firstPair = true;
0
@@ -112,7 +113,7 @@ class GeneratorMethodsLoader {
0
                     }
0
 
0
                     RubyString jsonKey = Utils.toJson(key.asString());
0
- result.cat(((RubyString)jsonKey).getByteList());
0
+ result.cat(jsonKey.getByteList());
0
                     result.infectBy(jsonKey);
0
                     result.cat((byte)':');
0
 
0
@@ -126,9 +127,7 @@ class GeneratorMethodsLoader {
0
         }
0
 
0
         private RubyString transform(RubyHash self, final GeneratorState state, int depth) {
0
- Ruby runtime = self.getRuntime();
0
- int preSize = 2 + Math.max(self.size() * 8, 0);
0
- final RubyString result = runtime.newString(new ByteList(preSize));
0
+ final Ruby runtime = self.getRuntime();
0
 
0
             final ByteList objectNl = state.object_nl_get().getByteList();
0
             final byte[] indent = Utils.repeat(state.indent_get().getByteList(), depth + 1);
0
@@ -136,10 +135,14 @@ class GeneratorMethodsLoader {
0
             final ByteList space = state.space_get().getByteList();
0
             final RubyFixnum subDepth = runtime.newFixnum(depth + 1);
0
 
0
+ final int preSize = 2 + self.size() * (12 + indent.length + spaceBefore.length() + space.length());
0
+ final RubyString result = runtime.newString(new ByteList(preSize));
0
+
0
             result.cat((byte)'{');
0
             result.cat(objectNl);
0
             self.visitAll(new RubyHash.Visitor() {
0
                 private boolean firstPair = true;
0
+
0
                 @Override
0
                 public void visit(IRubyObject key, IRubyObject value) {
0
                     // XXX key == Qundef???
0
@@ -150,9 +153,8 @@ class GeneratorMethodsLoader {
0
                         result.cat((byte)',');
0
                         result.cat(objectNl);
0
                     }
0
- if (objectNl.length() != 0) {
0
- result.cat(indent);
0
- }
0
+ if (objectNl.length() != 0) result.cat(indent);
0
+
0
                     RubyString keyJson = Utils.toJson(key.asString(), state, subDepth);
0
                     result.cat(keyJson.getByteList());
0
                     result.infectBy(keyJson);
...
86
87
88
89
90
91
 
 
92
93
94
...
123
124
125
126
 
 
127
128
129
...
133
134
135
136
137
 
 
138
139
140
...
143
144
145
146
147
148
 
149
150
151
...
86
87
88
 
 
 
89
90
91
92
93
...
122
123
124
 
125
126
127
128
129
...
133
134
135
 
 
136
137
138
139
140
...
143
144
145
 
 
 
146
147
148
149
0
@@ -86,9 +86,8 @@ final class Utils {
0
      * of the expected type
0
      */
0
     static GeneratorState ensureState(IRubyObject object) {
0
- if (object instanceof GeneratorState) {
0
- return (GeneratorState)object;
0
- }
0
+ if (object instanceof GeneratorState) return (GeneratorState)object;
0
+
0
         Ruby runtime = object.getRuntime();
0
         RubyClass generatorState =
0
             (RubyClass)runtime.getClassFromPath("JSON::Ext::Generator::State");
0
@@ -123,7 +122,8 @@ final class Utils {
0
         return newException(runtime, className, msg);
0
     }
0
 
0
- static RaiseException newException(Ruby runtime, String className, RubyString message) {
0
+ static RaiseException newException(Ruby runtime, String className,
0
+ RubyString message) {
0
         RubyClass klazz = runtime.getModule("JSON").getClass(className);
0
         RubyException excptn =
0
             (RubyException)klazz.newInstance(runtime.getCurrentContext(),
0
@@ -133,8 +133,8 @@ final class Utils {
0
     }
0
 
0
     /**
0
- * Invokes <code>to_json</code> on the given object and ensures it
0
- * returns a RubyString
0
+ * Invokes <code>to_json</code> on the given object and ensures
0
+ * it returns a RubyString
0
      * @param object The object to convert to JSON
0
      * @param args Parameters to pass to the method call
0
      * @return The {@link RubyString String} containing the
0
@@ -143,9 +143,7 @@ final class Utils {
0
     static RubyString toJson(IRubyObject object, IRubyObject... args) {
0
         Ruby runtime = object.getRuntime();
0
         IRubyObject result = object.callMethod(runtime.getCurrentContext(), "to_json", args);
0
- if (result instanceof RubyString) {
0
- return (RubyString)result;
0
- }
0
+ if (result instanceof RubyString) return (RubyString)result;
0
         throw runtime.newTypeError("to_json must return a String");
0
     }
0
 

Comments

    No one has commented yet.