mgholam / fastJSON

Smallest, fastest polymorphic JSON serializer
https://www.codeproject.com/Articles/159450/fastJSON-Smallest-Fastest-Polymorphic-JSON-Seriali
MIT License
479 stars 147 forks source link

Error: Operation could destabilize the runtime. #140

Closed frankinstien111 closed 1 year ago

frankinstien111 commented 1 year ago

I'm trying to serialize a complex object and I get this error:

Operation could destabilize the runtime. at _cgf(Object ) at fastJSON.JSONSerializer.WriteObject(Object obj) at fastJSON.JSONSerializer.WriteValue(Object obj) at fastJSON.JSONSerializer.WriteObject(Object obj) at fastJSON.JSONSerializer.WriteValue(Object obj) at fastJSON.JSONSerializer.ConvertToJSON(Object obj)

Below are the set of inheritable and nested classes of the object: ` [Serializable] public class BuildWikiPage : WebPage {

    [DataMember]
    public Source InforDescription { set; get; }

    [DataMember]
    public List<Tuple<string, string>> SeeAlso { set; get; }

    [DataMember]
    public List<String> Notes { set; get; }       

    public BuildWikiPage(HtmlDocument htmlDoc = null)            
    {

        Notes = new List<string>();
        SeeAlso = new List<Tuple<string, string>>();
        Headings = new List<Heading>();           
    }
}

[Serializable]
public class WebPage : Page
{
    [DataMember]
    public HtmlEmphasizer WordsLinked { set; get; }

    [DataMember]
    public List<Heading> Headings { set; get; }

    [DataMember]
    public SerializableDictionary<String, List<HtmlNode>> Tags { set; get; }

    public WebPage(HtmlDocument htmlDoc = null) 
        :base(null, null, Guid.Empty, 1)
    {
        Tags = new SerializableDictionary<string, List<HtmlNode>>(1000);
        WordsLinked = new HtmlEmphasizer(this);
        preParagraphs = new List<PreParagraph>();

    }
}

[Serializable]
public class Page : DocStructure
{        
    [DataMember]
    public int PageNumber { get; set; }

    [DataMember]
    public String PageTitle { get; set;}

    [DataMember]
    public List<Paragraph> Paragraphs { set; get; }

    [DataMember]
    public SerializableDictionary<String, List<EmphasizerIndexes>> WordEmphasisIndex { set; get; }
}

[Serializable]
public class Paragraph : DocStructure
{

    [DataMember]
    public List<Sentence> Sentences { set; get; }

    [DataMember]
    public bool IsList { set; get; }

    public Paragraph(Guid _id) :base(_id)
    {
        Sentences = new List<Sentence>();
    }

   public Page(Guid parentID) :base(parentID)
  {
        WordEmphasisIndex = new SerializableDictionary<string, List<EmphasizerIndexes>>(200);
        Paragraphs = new List<Paragraph>();
  }
}

[Serializable]
public class Sentence : DocStructure
{

   [DataMember]
    public SentenceTree PosStructure { set; get; }       

   [DataMember]
    public Punctuations Punctuation { private set; get; }

    public Sentence(Guid parentId) : base(parentId)
    {           

    }
 }

[Serializable]
public abstract class DocStructure : DocumentBase
{
    [DataMember]
    public string DataStore { get; set; }
    [DataMember]
    public int Ordinal { set; get; }

    public DocStructure(Guid parentId)
        :base(parentId) 
    {

    }
 }

[Serializable]
public  class DocumentBase : IDocumentBase
{      

    [DataMember]
    public Guid ParentGuidID { get;  set; }          
    [DataMember]
    public String Heading { set; get; }        
    [DataMember]
    public Guid ID { get;  set; }
    [DataMember]
    public int Ordinal { set; get; }
    [DataMember]
    public dynamic Tag { set; get; }     

    public DocumentBase(Guid parentId)
    {
        doneCount = 0;   
        ParentGuidID = parentId;
        ID = Guid.NewGuid();          
        collectionCount = 0;
    }
}

[Serializable]
public  class SentenceTree : PhraseGroup
{
    [DataMember]
    public String Title { private set; get; }

    [DataMember]
    public SerializableDictionary<Guid, PhraseGroup> PhaseGroups { get; set; }

    [DataMember]
    public SerializableDictionary<Guid, FlatWord> FlatWords { get; set; }

    public SentenceTree(Guid parent, string title, string baseId = null)
        :base(parent, baseId)
    {
        Title = title;
        PhaseGroups = new SerializableDictionary<Guid, PhraseGroup>(100);
        FlatWords = new SerializableDictionary<Guid, FlatWord>(100);
    }
}

[Serializable]
public class PhraseGroup : AbstractPOS, IPOSGroup
{

    [DataMember]
    public SerializableDictionary<Guid, AbstractPOS> ExposedFeatures { set; get; }

    public PhraseGroup(Guid parent, string baseId = null)
        :base(parent, baseId)
    {
        ExposedFeatures = new SerializableDictionary<Guid, AbstractPOS>(100);
    }
 }

[Serializable]
public class AbstractPOS : BaseElement
{
    [DataMember]
    public PennTreebankPOS PartOfSpeech { set; get; }        
    [DataMember]
    public bool IsWord { set; get; }
    public AbstractPOS(Guid parent, String baseID = null)
        :base(parent, baseID)
    {

    }
 }

[Serializable]
public  class BaseElement : IBaseElement
{
    [DataMember]
    public Guid ParentID { set; get; }

    [DataMember]
    public Guid BaseID {set; get; }

    [DataMember]
    public Guid ID { set; get; }

    [DataMember]
    public int IndexOcurrence { set; get; }

    [DataMember]
    public DateTime TimeStamp { set; get; }

    [DataMember]
    public DateTime LastModified { set; get; }

    [DataMember]
    public int PreComputedHasCode { set; get; }

    [DataMember]
    public bool HashCodePreComputed { set; get; }

    public BaseElement(Guid parent, string baseId = null)
    {
        ID = Guid.NewGuid();
        ParentID = parent;
        if (baseId == null)
            BaseID = ID;
        else
            BaseID = Guid.Parse(baseId);

        TimeStamp = DateTime.UtcNow;
        LastModified = TimeStamp;
    }
}

[Serializable]
public class FlatWord : AbstractPOS, IDocumentBase
{        
    [DataMember]
    public String Word { set; get; }
    [DataMember]
    public Guid ParentGuidID { set; get; }  

    [DataMember]
    public string Heading { set; get;  }

    [DataMember]
    public SentenceElementTypes ElementType { set; get; }

    [DataMember]
    public WordEmphasizers Emphasizer { set; get; }

    [DataMember]
    public int Ordinal {
        set
        {
            IndexOcurrence = value;
        }
        get
        {
            return IndexOcurrence;
        }
    }

    public FlatWord(Guid parent, string baseID = null)
        :base(parent, baseID)
    {
        IsWord = true;           
    }
}

`

mgholam commented 1 year ago

Try simplifying things...

frankinstien111 commented 1 year ago

The problem was due to the HtmlDocument object which is a part of the HtmlAgilityPack. The objects under that library are not necessary so I don't need to serialize them, but thought of it as nice to have.