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);
        }
Beispiel #2
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);
        }