Json.NET better way than Jsonignore to modify the source code

Source: Internet
Author: User

On the issue of Jsonignore,

The primary foreign key relationship exists in the EF T4 template

Namespace WindowsFormsApplication1
{
Using System;
Using System.Collections.Generic;
Using Newtonsoft.json;
public partial class Materielprocedures
{
public int Kid {get; set;}
Public nullable<int> Okid {get; set;}
public int Node {get; set;}
public string Proceduretype {get; set;}
public string Procedureneed {get; set;}
public string Author {get; set;}
Public System.DateTime createtime {get; set;}
Public nullable<system.datetime> StartTime {get; set;}
Public nullable<double> Planhour {get; set;}
Public nullable<system.datetime> EndTime {get; set;}
Public nullable<int> PercentAge {get; set;}
public string Procedurestate {get; set;}
public bool Isout {get; set;}
[Jsonignore]
Public virtual bom_2d bom_2d {get; set;}
}
}

BOM_2D is the foreign key of Materielprocedures, use Json.NET to turn Materielprocedures class into JSON string, because this foreign key error.

The workaround is in public virtual bom_2d bom_2d {get; set;} The line above adds [Jsonignore].

However, each time the template is regenerated, the Materielprocedures class is regenerated.

So directly to find the source of the problem, to modify, tracking N multi-Method finally

 Private voidSerializeObject (Jsonwriter writer,Objectvalue, jsonobjectcontract contract, Jsonproperty member, Jsoncontainercontract collectioncontract, JsonProperty            Containerproperty) {onserializing (writer, contract, value);            _serializestack.add (value);            Writeobjectstart (writer, value, contract, member, Collectioncontract, Containerproperty); intInitialdepth =writer.            Top;  for(intindex =0; Index < contract. Properties.Count; index++) {Jsonproperty property=contract.                Properties[index]; Try                {                    ObjectMembervalue;                    Jsoncontract membercontract; if(! Calculatepropertyvalues (writer, value, contract, Member, property, outMembercontract, outmembervalue)) Continue; #regionAdd by 253if(!membercontract.createdtype.basetype.fullname.contains ("System."))                        Continue; #endregionProperty .                    Writepropertyname (writer);                Serializevalue (writer, membervalue, Membercontract, property, contract, member); }                Catch(Exception ex) {if(iserrorhandled (value, contract, property.) PropertyName,NULL, writer.                    Containerpath, ex)) HandleError (writer, initialdepth); Else                        Throw; }            }            if(Contract. Extensiondatagetter! =NULL) {IEnumerable<KeyValuePair<Object,Object>> ExtensionData =contract.                Extensiondatagetter (value); if(ExtensionData! =NULL)                {                    foreach(keyvaluepair<Object,Object> EinchExtensionData) {jsoncontract Keycontract=Getcontractsafe (E.key); Jsoncontract valuecontract=Getcontractsafe (E.value); BOOLescape; stringPropertyName = Getpropertyname (writer, E.key, Keycontract, outescape); if(Shouldwritereference (E.value,NULL, Valuecontract, contract, member)) {writer.                            Writepropertyname (PropertyName);                        Writereference (writer, e.value); }                        Else                        {                            if(! Checkforcircularreference (writer, E.value,NULL, Valuecontract, contract, member)) Continue; Writer.                            Writepropertyname (PropertyName); Serializevalue (writer, e.value, Valuecontract,NULL, contract, member); }}}} writer.            Writeendobject (); _serializestack.removeat (_serializestack.count-1);        OnSerialized (writer, contract, value); }


It's done! Except for the data type under System, all continue!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.