      "classname", descriptor_->name());
  }
  
+@@ -477,4 +509,4 @@
+ }  // namespace javamicro
+ }  // namespace compiler
+ }  // namespace protobuf
+-}  // namespace google
++}  // namespace google
+\ No newline at end of file
+Index: src/google/protobuf/compiler/javamicro/javamicro_enum_field.h
+IDEA additional info:
+Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
+<+>UTF-8
+===================================================================
+--- src/google/protobuf/compiler/javamicro/javamicro_enum_field.h	(revision ce2f59915e0b41c935e3f72a2b7b71b6c19b6860)
++++ src/google/protobuf/compiler/javamicro/javamicro_enum_field.h	(revision )
+@@ -55,6 +55,7 @@
+   void GenerateParsingCode(io::Printer* printer) const;
+   void GenerateSerializationCode(io::Printer* printer) const;
+   void GenerateSerializedSizeCode(io::Printer* printer) const;
++  void GenerateToJSONCode(io::Printer* printer) const;
+ 
+   string GetBoxedType() const;
+ 
+@@ -76,6 +77,7 @@
+   void GenerateParsingCode(io::Printer* printer) const;
+   void GenerateSerializationCode(io::Printer* printer) const;
+   void GenerateSerializedSizeCode(io::Printer* printer) const;
++  void GenerateToJSONCode(io::Printer* printer) const;
+ 
+   string GetBoxedType() const;
+ 
+@@ -87,8 +89,4 @@
+ };
+ 
+ }  // namespace javamicro
+-}  // namespace compiler
+-}  // namespace protobuf
+-
+-}  // namespace google
+-#endif  // GOOGLE_PROTOBUF_COMPILER_JAVA_ENUM_FIELD_H__
++}  // namespace comp
+\ No newline at end of file
+Index: src/google/protobuf/compiler/javamicro/javamicro_message_field.cc
+IDEA additional info:
+Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
+<+>UTF-8
+===================================================================
+--- src/google/protobuf/compiler/javamicro/javamicro_message_field.cc	(revision ce2f59915e0b41c935e3f72a2b7b71b6c19b6860)
++++ src/google/protobuf/compiler/javamicro/javamicro_message_field.cc	(revision )
+@@ -78,6 +78,14 @@
+ MessageFieldGenerator::~MessageFieldGenerator() {}
+ 
+ void MessageFieldGenerator::
++	GenerateToJSONCode(io::Printer* printer) const {
++  printer->Print(variables_,
++	"if (has$capitalized_name$()) {\n"
++	"  stringer.key(\"$name$\").value(get$capitalized_name$().toJSON());\n"
++	"}\n");
++}
++
++void MessageFieldGenerator::
+ GenerateMembers(io::Printer* printer) const {
+   printer->Print(variables_,
+     "private boolean has$capitalized_name$;\n"
+@@ -89,7 +97,8 @@
+     "public $type$ get$capitalized_name$() { return $name$_; }\n"
+     "public $message_name$ set$capitalized_name$($type$ value) {\n"
+     "  if (value == null) {\n"
+-    "    throw new NullPointerException();\n"
++//    "    throw new NullPointerException();\n"
++    "    return clear$capitalized_name$();"
+     "  }\n"
+     "  has$capitalized_name$ = true;\n"
+     "  $name$_ = value;\n"
+@@ -159,6 +168,19 @@
+ RepeatedMessageFieldGenerator::~RepeatedMessageFieldGenerator() {}
+ 
+ void RepeatedMessageFieldGenerator::
++GenerateToJSONCode(io::Printer* printer) const {
++  printer->Print(variables_,
++	"count = get$capitalized_name$Count();\n"
++	"if (count > 0) {\n"
++	"  stringer.key(\"$name$\").array();\n"
++	"  for (int i = 0; i < count; ++i) {\n"
++	"    stringer.value(get$capitalized_name$(i).toJSON());\n"
++	"  }\n"
++	"  stringer.endArray();\n"
++	"}\n");
++}
++
++void RepeatedMessageFieldGenerator::
+ GenerateMembers(io::Printer* printer) const {
+   if (params_.java_use_vector()) {
+     printer->Print(variables_,
+@@ -172,14 +194,16 @@
+       "}\n"
+       "public $message_name$ set$capitalized_name$(int index, $type$ value) {\n"
+       "  if (value == null) {\n"
+-      "    throw new NullPointerException();\n"
++      //"    throw new NullPointerException();\n"
++      "    return this;\n"
+       "  }\n"
+       "  $name$_.setElementAt(value, index);\n"
+       "  return this;\n"
+       "}\n"
+       "public $message_name$ add$capitalized_name$($type$ value) {\n"
+       "  if (value == null) {\n"
+-      "    throw new NullPointerException();\n"
++      //"    throw new NullPointerException();\n"
++      "    return this;\n"
+       "  }\n"
+       "  $name$_.addElement(value);\n"
+       "  return this;\n"
+@@ -201,14 +225,16 @@
+       "}\n"
+       "public $message_name$ set$capitalized_name$(int index, $type$ value) {\n"
+       "  if (value == null) {\n"
+-      "    throw new NullPointerException();\n"
++      //"    throw new NullPointerException();\n"
++      "    return this;\n"
+       "  }\n"
+       "  $name$_.set(index, value);\n"
+       "  return this;\n"
+       "}\n"
+       "public $message_name$ add$capitalized_name$($type$ value) {\n"
+       "  if (value == null) {\n"
+-      "    throw new NullPointerException();\n"
++      //"    throw new NullPointerException();\n"
++      "    return this;\n"
+       "  }\n"
+       "  if ($name$_.isEmpty()) {\n"
+       "    $name$_ = new java.util.ArrayList<$type$>();\n"
+@@ -299,4 +325,4 @@
+ }  // namespace javamicro
+ }  // namespace compiler
+ }  // namespace protobuf
+-}  // namespace google
++}  // namespace google
+\ No newline at end of file
+Index: src/google/protobuf/compiler/javamicro/javamicro_message.h
+IDEA additional info:
+Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
+<+>UTF-8
+===================================================================
+--- src/google/protobuf/compiler/javamicro/javamicro_message.h	(revision ce2f59915e0b41c935e3f72a2b7b71b6c19b6860)
++++ src/google/protobuf/compiler/javamicro/javamicro_message.h	(revision )
+@@ -78,6 +78,8 @@
+   void GenerateClear(io::Printer* printer);
+   void GenerateIsInitialized(io::Printer* printer);
+ 
++  void GenerateToJSONCode(io::Printer* printer);
++
+   const Params& params_;
+   const Descriptor* descriptor_;
+   FieldGeneratorMap field_generators_;
+@@ -90,4 +92,4 @@
+ }  // namespace protobuf
+ 
+ }  // namespace google
+-#endif  // GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_H__
++#en
+\ No newline at end of file

Index: src/google/protobuf/compiler/javamicro/javamicro_primitive_field.cc
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/google/protobuf/compiler/javamicro/javamicro_primitive_field.cc	(revision 51ef8f39de376fe71ce5d2c682abe4f974cf8074)
+++ src/google/protobuf/compiler/javamicro/javamicro_primitive_field.cc	(revision )
@@ -198,8 +198,9 @@
   if (IsReferenceType(GetJavaType(descriptor))) {

   } else {
     (*variables)["null_check"] = "";
   }
@@ -222,6 +223,14 @@
 PrimitiveFieldGenerator::~PrimitiveFieldGenerator() {}
 
 void PrimitiveFieldGenerator::
+GenerateToJSONCode(io::Printer* printer) const {
+  printer->Print(variables_,
+	"if (has$capitalized_name$()) {\n"
+	"  stringer.key(\"$name$\").value(get$capitalized_name$());\n"
+	"}\n");
+}
+
+void PrimitiveFieldGenerator::
 GenerateMembers(io::Printer* printer) const {
   printer->Print(variables_,
     "private boolean has$capitalized_name$;\n"
@@ -339,6 +348,19 @@
 }
 
 RepeatedPrimitiveFieldGenerator::~RepeatedPrimitiveFieldGenerator() {}
+
+void RepeatedPrimitiveFieldGenerator::
+GenerateToJSONCode(io::Printer* printer) const {
+  printer->Print(variables_,
+	  "count = get$capitalized_name$Count();\n"
+	  "if (count > 0) {\n"
+	  "  stringer.key(\"$name$\").array();\n"
+	  "  for (int i = 0; i < count; ++i) {\n"
+	  "    stringer.value(get$capitalized_name$(i));\n"
+	  "  }\n"
+	  "  stringer.endArray();\n"
+	  "}\n");
+}
 
 void RepeatedPrimitiveFieldGenerator::
 GenerateMembers(io::Printer* printer) const {
@@ -673,4 +695,4 @@
 }  // namespace javamicro
 }  // namespace compiler
 }  // namespace protobuf
-}  // namespace google
+}  // namespace google
\ No newline at end of file
Index: src/google/protobuf/compiler/javamicro/javamicro_enum_field.cc
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/google/protobuf/compiler/javamicro/javamicro_enum_field.cc	(revision ce2f59915e0b41c935e3f72a2b7b71b6c19b6860)
+++ src/google/protobuf/compiler/javamicro/javamicro_enum_field.cc	(revision )
@@ -79,6 +79,14 @@
 EnumFieldGenerator::~EnumFieldGenerator() {}
 
 void EnumFieldGenerator::
+GenerateToJSONCode(io::Printer* printer) const {
+  printer->Print(variables_,
+    "if (has$capitalized_name$()) {\n"
+    "  stringer.key(\"$name$\").value(get$capitalized_name$());\n"
+    "}\n");
+}
+
+void EnumFieldGenerator::
 GenerateMembers(io::Printer* printer) const {
   printer->Print(variables_,
     "private boolean has$capitalized_name$;\n"
@@ -147,6 +155,19 @@
 RepeatedEnumFieldGenerator::~RepeatedEnumFieldGenerator() {}
 
 void RepeatedEnumFieldGenerator::
+GenerateToJSONCode(io::Printer* printer) const {
+  printer->Print(variables_,
+    "count = get$capitalized_name$Count();\n"
+    "if (count > 0) {\n"
+    "  stringer.key(\"$name$\").array();\n"
+    "  for (int i = 0; i < count; ++i) {\n"
+    "    stringer.value(get$capitalized_name$(i));\n"
+    "  }\n"
+    "  stringer.endArray();\n"
+    "}\n");
+}
+
+void RepeatedEnumFieldGenerator::
 GenerateMembers(io::Printer* printer) const {
   if (params_.java_use_vector()) {
     printer->Print(variables_,
@@ -306,28 +327,4 @@
         "if (get$capitalized_name$List().size() != 0) {"
         "  size += $tag_size$;\n"
         "  size += com.google.protobuf.micro.CodedOutputStreamMicro\n"
-        "    .computeRawVarint32Size(dataSize);\n"
-        "}");
-  } else {
-    printer->Print(variables_,
-        "size += $tag_size$ * get$capitalized_name$List().size();\n");
-  }
-
-  // cache the data size for packed fields.
-  if (descriptor_->options().packed()) {
-    printer->Print(variables_,
-      "$name$MemoizedSerializedSize = dataSize;\n");
-  }
-
-  printer->Outdent();
-  printer->Print("}\n");
-}
-
-string RepeatedEnumFieldGenerator::GetBoxedType() const {
-  return ClassName(params_, descriptor_->enum_type());
-}
-
-}  // namespace javamicro
-}  // namespace compiler
-}  // namespace protobuf
-}  // namespace google
+        "    .
\ No newline at end of file
Index: src/google/protobuf/compiler/javamicro/javamicro_field.h
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/google/protobuf/compiler/javamicro/javamicro_field.h	(revision ce2f59915e0b41c935e3f72a2b7b71b6c19b6860)
+++ src/google/protobuf/compiler/javamicro/javamicro_field.h	(revision )
@@ -62,6 +62,7 @@
   virtual void GenerateParsingCode(io::Printer* printer) const = 0;
   virtual void GenerateSerializationCode(io::Printer* printer) const = 0;
   virtual void GenerateSerializedSizeCode(io::Printer* printer) const = 0;
+  virtual void GenerateToJSONCode(io::Printer* printer) const = 0;
 
   virtual string GetBoxedType() const = 0;
 
@@ -94,5 +95,4 @@
 }  // namespace compiler
 }  // namespace protobuf
 
-}  // namespace google
-#endif  // GOOGLE_PROTOBUF_COMPILER_JAVA_FIELD_H__
+}  // n
\ No newline at end of file
Index: src/google/protobuf/compiler/javamicro/javamicro_message_field.h
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/google/protobuf/compiler/javamicro/javamicro_message_field.h	(revision ce2f59915e0b41c935e3f72a2b7b71b6c19b6860)
+++ src/google/protobuf/compiler/javamicro/javamicro_message_field.h	(revision )
@@ -55,6 +55,7 @@
   void GenerateParsingCode(io::Printer* printer) const;
   void GenerateSerializationCode(io::Printer* printer) const;
   void GenerateSerializedSizeCode(io::Printer* printer) const;
+  void GenerateToJSONCode(io::Printer* printer) const;
 
   string GetBoxedType() const;
 
@@ -77,6 +78,7 @@
   void GenerateParsingCode(io::Printer* printer) const;
   void GenerateSerializationCode(io::Printer* printer) const;
   void GenerateSerializedSizeCode(io::Printer* printer) const;
+  void GenerateToJSONCode(io::Printer* printer) const;
 
   string GetBoxedType() const;
 
@@ -88,8 +90,4 @@
 };
 
 }  // namespace javamicro
-}  // namespace compiler
-}  // namespace protobuf
-
-}  // namespace google
-#endif  // GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_FIELD_H__
+}  // namespace compile
\ No newline at end of file
Index: src/google/protobuf/compiler/javamicro/javamicro_message.cc
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/google/protobuf/compiler/javamicro/javamicro_message.cc	(revision ce2f59915e0b41c935e3f72a2b7b71b6c19b6860)
+++ src/google/protobuf/compiler/javamicro/javamicro_message.cc	(revision )
@@ -219,6 +219,7 @@
   GenerateMessageSerializationMethods(printer);
   GenerateMergeFromMethods(printer);
   GenerateParseFromMethods(printer);
+  GenerateToJSONCode(printer);
 
   printer->Outdent();
   printer->Print("}\n\n");
@@ -352,6 +353,27 @@
     "\n");
 }
 
+void MessageGenerator::GenerateToJSONCode(io::Printer* printer) {
+  scoped_array<const FieldDescriptor*> sorted_fields(
+    SortFieldsByNumber(descriptor_));
+  printer->Print(
+    "public String toJSON() throws org.json.JSONException {\n"
+    "  org.json.JSONStringer stringer = new org.json.JSONStringer();\n"
+    "  stringer.object();\n"
+    "  int count;\n");
+  printer->Indent();
+
+  for (int i = 0; i < descriptor_->field_count(); i++) {
+    field_generators_.get(sorted_fields[i]).GenerateToJSONCode(printer);
+  }
+
+  printer->Print(
+    "stringer.endObject();\n"
+    "return stringer.toString();\n");
+  printer->Outdent();
+  printer->Print("}\n");
+}
+
 void MessageGenerator::
 GenerateParseFromMethods(io::Printer* printer) {
   // Note:  These are separate from GenerateMessageSerializationMethods()
@@ -363,12 +385,22 @@
     "  return ($classname$) (new $classname$().mergeFrom(data));\n"
     "}\n"
     "\n"
