public Dictionary<string, Defaults4Class> getAlias2Defaults(TypeAliaser typeAliaser)
 {
     Dictionary<string, Defaults4Class> result = new Dictionary<string, Defaults4Class>();
     foreach (Type type in Type2Defaults.Keys)
     {
         string alias = typeAliaser(type);
         result[alias] = Type2Defaults[type];
     }
     return result;
 }
        public Dictionary <string, Defaults4Class> getAlias2Defaults(TypeAliaser typeAliaser)
        {
            Dictionary <string, Defaults4Class> result = new Dictionary <string, Defaults4Class>();

            foreach (Type type in Type2Defaults.Keys)
            {
                string alias = typeAliaser(type);
                result[alias] = Type2Defaults[type];
            }
            return(result);
        }
Esempio n. 3
0
        public string getLeafDefaultResolverJS(TypeAliaser typeAliaser)
        {
            //switch off type aliasing

            string defaultsJS      = toJson(LeafDefaultSet.getAlias2Defaults(typeAliaser), null);
            string defaultsVarName = "_typeAlias2LeafDefaults_";

            string js =
                @"var " + defaultsVarName + " = " + defaultsJS + @";
" + Object2JsonJavascript.ResolveLeafDefaultFunction;

            return(js);
        }
        public string toJson(Object o, TypeAliaser typeAliaser)
        {
            /**
                o2J.OmitDefaultLeafValuesInJs = true;
                o2J.LeafDefaultSet = lds;
            //  to do throw exception if this is not the case
            //  o2J.OmitMarkAsArrayFunction = false;
           **/

         
            StringWriter sw = new StringWriter();
            writeAsJson(o, sw, typeAliaser);
            sw.Flush();
            String str = sw.ToString();
            sw.Close();
            return str;
        }
Esempio n. 5
0
/**        Need to take note of AlwaysDoubleQuotePropertyNames when writing property type
 * Always escape property name with double quotes
 **/
        int writeTypeAliasProperty(TextWriter writer, Object to, TypeAliaser typeAliaser, int currentFramePropertyCount)
        {
            if (typeAliaser != null)
            {
                if (currentFramePropertyCount > 0)
                {
                    writer.Write(',');
                }
                string strAliasPropertyWrapper = AlwaysDoubleQuotePropertyNames ? "\"" : "";
                writer.Write(strAliasPropertyWrapper + this.TypeAliasProperty + strAliasPropertyWrapper + ":\"" + typeAliaser(to.GetType()) + "\"");
                return(1);
            }
            else
            {
                return(0);
            }
        }
Esempio n. 6
0
        public string toJson(Object o, TypeAliaser typeAliaser)
        {
            /**
             *  o2J.OmitDefaultLeafValuesInJs = true;
             *  o2J.LeafDefaultSet = lds;
             * //  to do throw exception if this is not the case
             * //  o2J.OmitMarkAsArrayFunction = false;
             **/


            StringWriter sw = new StringWriter();

            writeAsJson(o, sw, typeAliaser);
            sw.Flush();
            String str = sw.ToString();

            sw.Close();
            return(str);
        }
        public static TypeAliaser createNumericTypeNameAliaser()
        {
            Dictionary <Type, string> type2Alias = new Dictionary <Type, string>();
            // try a different
            TypeAliaser aliaser = delegate(Type type)
            {
                string alias;
                if (!type2Alias.ContainsKey(type))
                {
                    alias            = "_" + type2Alias.Keys.Count;
                    type2Alias[type] = alias;
                }
                else
                {
                    alias = type2Alias[type];
                }
                return(alias);
            };

            return(aliaser);
        }
    public string getLeafDefaultResolverJS(TypeAliaser typeAliaser)
    {
        //switch off type aliasing
        
        string defaultsJS = toJson( LeafDefaultSet.getAlias2Defaults(typeAliaser), null);
        string defaultsVarName = "_typeAlias2LeafDefaults_";

        string js =
@"var "+ defaultsVarName +" = " + defaultsJS + @";
" + Object2JsonJavascript.ResolveLeafDefaultFunction;           
        return js;
    }
/**        Need to take note of AlwaysDoubleQuotePropertyNames when writing property type
Always escape property name with double quotes
**/
 int writeTypeAliasProperty(TextWriter writer, Object to, TypeAliaser typeAliaser, int currentFramePropertyCount)
        {
            if (typeAliaser != null)
            {
                if (currentFramePropertyCount > 0) writer.Write(',');
                string strAliasPropertyWrapper = AlwaysDoubleQuotePropertyNames ? "\"" : "";
                writer.Write(strAliasPropertyWrapper + this.TypeAliasProperty + strAliasPropertyWrapper + ":\"" + typeAliaser(to.GetType()) + "\"");
                return 1;
            }
            else
            {
                return 0;
            }
        }
        public void writeAsJson(Object o, TextWriter writer, TypeAliaser typeAliaser=null)
        {
            if (LeafDefaultSet != null && OmitDefaultLeafValuesInJs && OmitMarkAsArrayFunction)
            {
                throw new Exception("Leaf defaulting requires Array marker for js code");
            }


            ObjectIDGenerator idGenerator = null;

            if (UseReferences)
            {
                idGenerator=new ObjectIDGenerator();
            }
            Stack<ExploreStackFrame> exploreStack = new Stack<ExploreStackFrame>();
            Explorer explorerImpl = ExplorerFactory();
            ((Explorer)explorerImpl).NodeExpander = NodeExpander;
            MoveAway down = delegate (Object from, string propertyName, Object to, bool isIndexed, int? index)
            {
                ExploreStackFrame currentFrame = exploreStack.Count>0 ? exploreStack.Peek():null;
                if (currentFrame != null)
                {
                    if (currentFrame.propertyCount > 0) writer.Write(", ");
                    currentFrame.propertyCount++;
                }
                if (from != null && propertyName != null)
                {
                    writePropertyName(propertyName, writer);
                    writer.Write(":");
                }
                ExploreStackFrame childFrame = new ExploreStackFrame();
                exploreStack.Push(childFrame);
                writeIndent(writer, exploreStack);
                if (UseReferences)
                {
                    bool firstTime;
                    long objectid = idGenerator.GetId(to, out firstTime);
                    if (firstTime)
                    {
                        // could be done like this ! (function() {var x=[1,2]; x.id="uuu";return x;})()
                        if (!isIndexed)
                        {
                            writer.Write("{" + this.IdTag + ":" + objectid + ' ');
                            childFrame.propertyCount++;
                            childFrame.propertyCount += writeTypeAliasProperty(writer, to, typeAliaser, childFrame.propertyCount);
                            
                        }
                        else
                        {
                            // no need for type alias
                            writer.Write(AttachId2ArrayFunctionName + "(" + objectid + ",[");
                        }
                        
                    }
                    else
                    {
                        writer.Write("{" + this.ReferenceTag + ":" + objectid);
                        return false;
                    }
                }
                else // !Use References
                {
                    if (!isIndexed)
                    {
                        writer.Write('{');
                        // todo -- check this out ............
                        childFrame.propertyCount += writeTypeAliasProperty(writer, to, typeAliaser, childFrame.propertyCount);
                    }
                    else
                    {
                        if (!OmitMarkAsArrayFunction)
                        {
                            writer.Write(markAsArrayFunctionName);
                            writer.Write("([");
                        }
                        else
                        {
                            writer.Write("[");
                        }
                    }
                }

                return true;
            };

            MoveBack up = (from, propertyName, to, isIndexed) => 
                {
                    if (!isIndexed) writer.Write('}');
                    else
                    {
                        writer.Write(']');
                        // is there a function wrapper ?
                        if (!OmitMarkAsArrayFunction || UseReferences) writer.Write(")");
                    }
                    exploreStack.Pop();
                    writeIndent(writer, exploreStack);
                };

            OnLeaf leaf = (from, propertyName, to, index) => 
              {
                  //check for default leaf values
                  if (!this.OmitDefaultLeafValuesInJs ||
                      !isDefaultLeafValue(from, propertyName, to, LeafDefaultSet))
                  {
                     ExploreStackFrame currentFrame = exploreStack.Peek();
                     if (currentFrame.propertyCount > 0) writer.Write(", ");
                     currentFrame.propertyCount++;
                     if (propertyName!=null)
                     {
                         writePropertyName(propertyName, writer);
                         writer.Write(":");                     
                     }
                  writeLeafValue(writer, to, propertyName);
                  }
              };
            explorerImpl.explore(o, down, up, leaf);
        }
Esempio n. 11
0
        public void writeAsJson(Object o, TextWriter writer, TypeAliaser typeAliaser = null)
        {
            if (LeafDefaultSet != null && OmitDefaultLeafValuesInJs && OmitMarkAsArrayFunction)
            {
                throw new Exception("Leaf defaulting requires Array marker for js code");
            }


            ObjectIDGenerator idGenerator = null;

            if (UseReferences)
            {
                idGenerator = new ObjectIDGenerator();
            }
            Stack <ExploreStackFrame> exploreStack = new Stack <ExploreStackFrame>();
            Explorer explorerImpl = ExplorerFactory();

            ((Explorer)explorerImpl).NodeExpander = NodeExpander;
            MoveAway down = delegate(Object from, string propertyName, Object to, bool isIndexed, int?index)
            {
                ExploreStackFrame currentFrame = exploreStack.Count > 0 ? exploreStack.Peek():null;
                if (currentFrame != null)
                {
                    if (currentFrame.propertyCount > 0)
                    {
                        writer.Write(", ");
                    }
                    currentFrame.propertyCount++;
                }
                if (from != null && propertyName != null)
                {
                    writePropertyName(propertyName, writer);
                    writer.Write(":");
                }
                ExploreStackFrame childFrame = new ExploreStackFrame();
                exploreStack.Push(childFrame);
                writeIndent(writer, exploreStack);
                if (UseReferences)
                {
                    bool firstTime;
                    long objectid = idGenerator.GetId(to, out firstTime);
                    if (firstTime)
                    {
                        // could be done like this ! (function() {var x=[1,2]; x.id="uuu";return x;})()
                        if (!isIndexed)
                        {
                            writer.Write("{" + this.IdTag + ":" + objectid + ' ');
                            childFrame.propertyCount++;
                            childFrame.propertyCount += writeTypeAliasProperty(writer, to, typeAliaser, childFrame.propertyCount);
                        }
                        else
                        {
                            // no need for type alias
                            writer.Write(AttachId2ArrayFunctionName + "(" + objectid + ",[");
                        }
                    }
                    else
                    {
                        writer.Write("{" + this.ReferenceTag + ":" + objectid);
                        return(false);
                    }
                }
                else // !Use References
                {
                    if (!isIndexed)
                    {
                        writer.Write('{');
                        // todo -- check this out ............
                        childFrame.propertyCount += writeTypeAliasProperty(writer, to, typeAliaser, childFrame.propertyCount);
                    }
                    else
                    {
                        if (!OmitMarkAsArrayFunction)
                        {
                            writer.Write(markAsArrayFunctionName);
                            writer.Write("([");
                        }
                        else
                        {
                            writer.Write("[");
                        }
                    }
                }

                return(true);
            };

            MoveBack up = (from, propertyName, to, isIndexed) =>
            {
                if (!isIndexed)
                {
                    writer.Write('}');
                }
                else
                {
                    writer.Write(']');
                    // is there a function wrapper ?
                    if (!OmitMarkAsArrayFunction || UseReferences)
                    {
                        writer.Write(")");
                    }
                }
                exploreStack.Pop();
                writeIndent(writer, exploreStack);
            };

            OnLeaf leaf = (from, propertyName, to, index) =>
            {
                //check for default leaf values
                if (!this.OmitDefaultLeafValuesInJs ||
                    !isDefaultLeafValue(from, propertyName, to, LeafDefaultSet))
                {
                    ExploreStackFrame currentFrame = exploreStack.Peek();
                    if (currentFrame.propertyCount > 0)
                    {
                        writer.Write(", ");
                    }
                    currentFrame.propertyCount++;
                    if (propertyName != null)
                    {
                        writePropertyName(propertyName, writer);
                        writer.Write(":");
                    }
                    writeLeafValue(writer, to, propertyName);
                }
            };

            explorerImpl.explore(o, down, up, leaf);
        }