From 9b0e7f8b84298b284fe79aaa317ed36394921a24 Mon Sep 17 00:00:00 2001 From: Inderjeet Singh Date: Tue, 14 Sep 2010 17:59:04 +0000 Subject: [PATCH] initial check-in for implementing support for protocol buffers in Gson --- proto/pom.xml | 251 ++++++++++++++++++ .../gson/protobuf/ProtoTypeAdapter.java | 69 +++++ proto/src/main/protobuf/bag.proto | 23 ++ .../google/gson/protobuf/FunctionalTest.java | 54 ++++ wsf/pom.xml | 4 +- 5 files changed, 399 insertions(+), 2 deletions(-) create mode 100644 proto/pom.xml create mode 100644 proto/src/main/java/com/google/gson/protobuf/ProtoTypeAdapter.java create mode 100644 proto/src/main/protobuf/bag.proto create mode 100644 proto/src/test/java/com/google/gson/protobuf/FunctionalTest.java diff --git a/proto/pom.xml b/proto/pom.xml new file mode 100644 index 00000000..95545349 --- /dev/null +++ b/proto/pom.xml @@ -0,0 +1,251 @@ + + + + + 4.0.0 + com.google.code.gson + proto + jar + 0.1 + Gson Protobuf Support + Gson support for Protobufs + + + local.repo + file repository to svn + file://${basedir}/../../mavenrepo + + + + + gson + http://google-gson.googlecode.com/svn/mavenrepo + + true + + + true + + + + + + The Apache Software License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + repo + + + + scm:svn:http://google-gson.googlecode.com/svn/trunk/proto + scm:svn:https://google-gson.googlecode.com/svn/trunk/proto + http://google-gson.codegoogle.com/svn/trunk/proto + + + Google Code Issue Tracking + http://code.google.com/p/google-gson/issues/list + + + + + + + com.google.code.gson + gson + 1.5 + compile + + + + com.google.protobuf + protobuf-java + 2.3.0 + compile + + + + junit + junit + 3.8.2 + test + + + + + gson-proto + + + maven-antrun-plugin + + + compile-protoc + generate-sources + + + + + + + + + + + + + + + + + + run + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.0.2 + + 1.6 + 1.6 + + + + org.apache.maven.plugins + maven-eclipse-plugin + 2.5.1 + + true + true + ../eclipse-ws + file:///${basedir}/../lib/gson-formatting-styles.xml + 1.5 + + + + org.apache.maven.plugins + maven-install-plugin + 2.2 + + + + org.mortbay.jetty + maven-jetty-plugin + 6.0.1 + + 10 + + + + org.apache.maven.plugins + maven-jxr-plugin + + + + org.apache.maven.plugins + maven-source-plugin + + + attach-sources + verify + + jar + + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + + src/main/resources/javadoc-descriptor.xml + + http://java.sun.com/j2se/1.5.0/docs/api/ + + true + true + public + + + + org.apache.maven.plugins + maven-pmd-plugin + + 1.5 + + /rulesets/basic.xml + /rulesets/imports.xml + /rulesets/unusedcode.xml + /rulesets/finalizers.xml + + + + + org.apache.maven.plugins + maven-checkstyle-plugin + + + config/maven_checks.xml + + + + org.codehaus.mojo + taglist-maven-plugin + + + TODO + @todo + FIXME + XXX + + + + + org.codehaus.mojo + cobertura-maven-plugin + + + clean + + clean + + + + + + org.apache.maven.plugins + maven-release-plugin + + -DenableCiProfile=true + + + + maven-assembly-plugin + 2.2-beta-2 + + src/main/resources/assembly-descriptor.xml + proto-${version} + target/dist + target/assembly/work + + + + + + + Inderjeet Singh + Google Inc. + + + diff --git a/proto/src/main/java/com/google/gson/protobuf/ProtoTypeAdapter.java b/proto/src/main/java/com/google/gson/protobuf/ProtoTypeAdapter.java new file mode 100644 index 00000000..1fbb0e34 --- /dev/null +++ b/proto/src/main/java/com/google/gson/protobuf/ProtoTypeAdapter.java @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2010 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.gson.protobuf; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; +import com.google.protobuf.Descriptors.FieldDescriptor; +import com.google.protobuf.Descriptors.FieldDescriptor.JavaType; +import com.google.protobuf.Message; + +import java.lang.reflect.Type; +import java.util.Map; + +public class ProtoTypeAdapter implements JsonSerializer { + + @Override + public JsonElement serialize(Message msg, Type typeOfMsg, JsonSerializationContext context) { + JsonObject obj = new JsonObject(); + Map allFields = msg.getAllFields(); + for (Map.Entry entry : allFields.entrySet()) { + FieldDescriptor key = entry.getKey(); + Object value = entry.getValue(); + JavaType javaType = key.getJavaType(); + Class type = toJavaType(javaType); + JsonElement element = context.serialize(value, type); + obj.add(key.getName(), element); + } + return obj; + } + + private Class toJavaType(JavaType javaType) { + switch (javaType) { + case BOOLEAN: + return Boolean.class; + case BYTE_STRING: + return String.class; + case DOUBLE: + return double.class; + case ENUM: + return Enum.class; + case FLOAT: + return float.class; + case INT: + return int.class; + case LONG: + return long.class; + case MESSAGE: + return Message.class; + case STRING: + return String.class; + } + return Object.class; + } +} diff --git a/proto/src/main/protobuf/bag.proto b/proto/src/main/protobuf/bag.proto new file mode 100644 index 00000000..c66b8b55 --- /dev/null +++ b/proto/src/main/protobuf/bag.proto @@ -0,0 +1,23 @@ +// +// Copyright (C) 2010 Google Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +package com.google.gson.protobuf.generated; +option java_package = "com.google.gson.protobuf.generated"; + +message SimpleProto { + optional string msg = 1; + optional int64 count = 2; +} diff --git a/proto/src/test/java/com/google/gson/protobuf/FunctionalTest.java b/proto/src/test/java/com/google/gson/protobuf/FunctionalTest.java new file mode 100644 index 00000000..692c7185 --- /dev/null +++ b/proto/src/test/java/com/google/gson/protobuf/FunctionalTest.java @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2010 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.gson.protobuf; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.protobuf.generated.Bag.SimpleProto; +import com.google.protobuf.Message; +import com.google.protobuf.Descriptors.Descriptor; + +import junit.framework.TestCase; + +public class FunctionalTest extends TestCase { + private Gson gson; + + @Override + protected void setUp() throws Exception { + super.setUp(); + gson = new GsonBuilder() +// .registerTypeHierarchyAdapter(Message.class, new ProtoTypeAdapter()) + .create(); + } + + public void testSerializeEmptyProto() { + SimpleProto proto = SimpleProto.newBuilder().build(); + String json = gson.toJson(proto); + assertEquals("{}", json); + } + + public void testSerializeProto() { + Descriptor descriptor = SimpleProto.getDescriptor(); + SimpleProto proto = SimpleProto.newBuilder() + .setField(descriptor.findFieldByNumber(1), "foo") + .setField(descriptor.findFieldByNumber(2), 3) + .build(); + String json = gson.toJson(proto); + System.out.println(json); + assertTrue(json.contains("\"msg\":\"foo\"")); + assertTrue(json.contains("\"count\":3")); + } +} diff --git a/wsf/pom.xml b/wsf/pom.xml index 3a923b38..2fac729c 100755 --- a/wsf/pom.xml +++ b/wsf/pom.xml @@ -45,9 +45,9 @@ Google Code Issue Tracking http://code.google.com/p/google-gson/issues/list - + - + com.google.collections