directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kayyag...@apache.org
Subject svn commit: r1510432 - in /directory/escimo/trunk: common/src/main/java/org/apache/directory/scim/json/ ldap/src/main/java/org/apache/directory/scim/json/
Date Mon, 05 Aug 2013 11:31:11 GMT
Author: kayyagari
Date: Mon Aug  5 11:31:10 2013
New Revision: 1510432

URL: http://svn.apache.org/r1510432
Log:
moved json serializer to common

Added:
    directory/escimo/trunk/common/src/main/java/org/apache/directory/scim/json/
    directory/escimo/trunk/common/src/main/java/org/apache/directory/scim/json/ResourceSerializer.java
    directory/escimo/trunk/common/src/main/java/org/apache/directory/scim/json/SimpleAttributeDeSerializer.java
Removed:
    directory/escimo/trunk/ldap/src/main/java/org/apache/directory/scim/json/

Added: directory/escimo/trunk/common/src/main/java/org/apache/directory/scim/json/ResourceSerializer.java
URL: http://svn.apache.org/viewvc/directory/escimo/trunk/common/src/main/java/org/apache/directory/scim/json/ResourceSerializer.java?rev=1510432&view=auto
==============================================================================
--- directory/escimo/trunk/common/src/main/java/org/apache/directory/scim/json/ResourceSerializer.java
(added)
+++ directory/escimo/trunk/common/src/main/java/org/apache/directory/scim/json/ResourceSerializer.java
Mon Aug  5 11:31:10 2013
@@ -0,0 +1,124 @@
+/*
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *   or more contributor license agreements.  See the NOTICE file
+ *   distributed with this work for additional information
+ *   regarding copyright ownership.  The ASF licenses this file
+ *   to you 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 org.apache.directory.scim.json;
+
+
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.directory.scim.AbstractAttribute;
+import org.apache.directory.scim.ComplexAttribute;
+import org.apache.directory.scim.MultiValAttribute;
+import org.apache.directory.scim.SimpleAttribute;
+import org.apache.directory.scim.SimpleAttributeGroup;
+import org.apache.directory.scim.User;
+
+import com.google.gson.Gson;
+import com.google.gson.JsonArray;
+import com.google.gson.JsonObject;
+
+
+/**
+ * TODO ResourceSerializer.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class ResourceSerializer
+{
+    private Gson gson = new Gson();
+
+    public static String serialize( User user )
+    {
+        JsonObject json = new JsonObject();
+
+        Map<String, List<AbstractAttribute>> attributes = user.getAttributes();
+        for ( String uri : attributes.keySet() )
+        {
+            serialize( json, attributes.get( uri ) );
+        }
+        
+        return json.toString();
+    }
+
+
+    public static void serialize( JsonObject parent, List<AbstractAttribute> attributes
)
+    {
+        for ( AbstractAttribute at : attributes )
+        {
+            serializeAt( parent, at );
+        }
+    }
+
+
+    public static void serializeAt( JsonObject parent, AbstractAttribute at )
+    {
+        if ( at instanceof SimpleAttribute )
+        {
+            serializeSimpleAt( parent, ( SimpleAttribute ) at );
+        }
+        else if ( at instanceof ComplexAttribute )
+        {
+            ComplexAttribute ct = ( ComplexAttribute ) at;
+            JsonObject json = new JsonObject();
+            for( SimpleAttribute t : ct.getAtList() )
+            {
+                serializeSimpleAt( json, t );
+            }
+            
+            parent.add( ct.getName(), json );
+        }
+        else if ( at instanceof MultiValAttribute )
+        {
+            MultiValAttribute mv = ( MultiValAttribute ) at;
+            List<SimpleAttributeGroup> lstGrp = mv.getAtGroupList();
+            
+            JsonArray array = new JsonArray();
+            
+            for( SimpleAttributeGroup stg : lstGrp )
+            {
+                JsonObject json = new JsonObject();
+                for( SimpleAttribute t : stg.getAtList() )
+                {
+                    serializeSimpleAt( json, t );
+                }
+                
+                array.add( json );
+            }
+            
+            parent.add( mv.getName(), array );
+        }
+    }
+
+
+    public static void serializeSimpleAt( JsonObject parent, SimpleAttribute at )
+    {
+        Object obj = at.getValue();
+
+        if ( obj instanceof String )
+        {
+            parent.addProperty( at.getName(), ( String ) obj );
+        }
+        else if ( obj instanceof Number )
+        {
+            parent.addProperty( at.getName(), ( Number ) obj );
+        }
+    }
+}

Added: directory/escimo/trunk/common/src/main/java/org/apache/directory/scim/json/SimpleAttributeDeSerializer.java
URL: http://svn.apache.org/viewvc/directory/escimo/trunk/common/src/main/java/org/apache/directory/scim/json/SimpleAttributeDeSerializer.java?rev=1510432&view=auto
==============================================================================
--- directory/escimo/trunk/common/src/main/java/org/apache/directory/scim/json/SimpleAttributeDeSerializer.java
(added)
+++ directory/escimo/trunk/common/src/main/java/org/apache/directory/scim/json/SimpleAttributeDeSerializer.java
Mon Aug  5 11:31:10 2013
@@ -0,0 +1,61 @@
+/*
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *   or more contributor license agreements.  See the NOTICE file
+ *   distributed with this work for additional information
+ *   regarding copyright ownership.  The ASF licenses this file
+ *   to you 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 org.apache.directory.scim.json;
+
+import java.lang.reflect.Type;
+import java.util.Iterator;
+import java.util.Map.Entry;
+
+import org.apache.directory.scim.SimpleAttribute;
+
+import com.google.gson.JsonDeserializationContext;
+import com.google.gson.JsonDeserializer;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParseException;
+
+/**
+ * TODO SimpleAttributeDeSerializer.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class SimpleAttributeDeSerializer implements JsonDeserializer<SimpleAttribute>
+{
+
+    @Override
+    public SimpleAttribute deserialize( JsonElement elm, Type jsonType, JsonDeserializationContext
ctx )
+        throws JsonParseException
+    {
+        JsonObject json = elm.getAsJsonObject();
+
+        Iterator<Entry<String, JsonElement>> itr = json.entrySet().iterator();
+        
+        if( itr.hasNext() )
+        {
+            Entry<String, JsonElement> entry = itr.next();
+            
+            SimpleAttribute at = new SimpleAttribute( entry.getKey(), entry.getValue().getAsString()
);
+        }
+        
+        return null;
+    }
+
+}



Mime
View raw message