The Newtonsoft. Json component converts object2json to newtonsoft. json.
In many cases, we need to convert the data type for calling by other external subsystems.
The most typical is to generate json format for javascript calls.
The ready-made component Newtonsoft. Json can be converted between object2json.
Newtonsoft. Json. JavaScriptConvert. SerializeObject (object) can be serialized in json format and is also a deserialization method.
Common scenarios:
System A provides user information (MemberInfo) for sub-system B to call, but some of the user information cannot be disclosed, such as the Email address.
This article is published on www.webjx.com! Do not remove reprint and collection! Thank you.
Using Newtonsoft. Json. JavaScriptConvert. SerializeObject directly does not seem to work. It lists all the attributes of the object.
My approach is as follows:
Defines a feature class that can only be used for class attributes.
View plaincopy to clipboardprint?
[AttributeUsage (AttributeTargets. Property)]
Public class DenyReflectionAttrubite: Attribute
{
Public DenyReflectionAttrubite (){}
}
[AttributeUsage (AttributeTargets. Property)]
Public class DenyReflectionAttrubite: Attribute
{
Public DenyReflectionAttrubite (){}
}
MemberInfo class
View plaincopy to clipboardprint?
Public class MemberInfo
{
Public int Uid
{
Get;
Set;
}
Public string UserName
{
Get;
Set;
}
Public string NickName
{
Get;
Set;
}
[DenyReflectionAttrubite]
Public string Password
{
Get;
Set;
}
[DenyReflectionAttrubite]
Public string Email
{
Get;
Set;
}
//.......................
}
Public class MemberInfo
{
Public int Uid
{
Get;
Set;
}
Public string UserName
{
Get;
Set;
}
Public string NickName
{
Get;
Set;
}
[DenyReflectionAttrubite]
Public string Password
{
Get;
Set;
}
[DenyReflectionAttrubite]
Public string Email
{
Get;
Set;
}
//.......................
}
The following section describes how to use the DenyReflectionAttrubite feature.
Json generation
View plaincopy to clipboardprint?
Public void Builder ()
{
Using (jsonWriter = new JsonTextWriter (sw ))
{
JsonWriter. Formatting = Formatting. None;
JsonWriter. Indentation = 4;
JsonWriter. WriteStartObject ();
JsonWriter. WritePropertyName ("member ");
JsonWriter. WriteStartArray ();
JsonWriter. WriteStartObject ();
Type settingsType = this. member. GetType ();
Foreach (PropertyInfo propertyInformation in settingsType. GetProperties ())
{
Try
{
// Skip the DenyReflectionAttrubite attribute here
If (propertyInformation. GetCustomAttributes (typeof (DenyReflectionAttrubite), false). Length> 0) continue;
Object propertyValue = propertyInformation. GetValue (member, null );
String valueAsString = propertyValue. ToString ();
If (propertyValue. Equals (null ))
{
ValueAsString = String. Empty;
}
If (propertyValue. Equals (Int32.MinValue ))
{
ValueAsString = String. Empty;
}
If (propertyValue. Equals (Single. MinValue ))
{
ValueAsString = String. Empty;
}
JsonWriter. WritePropertyName (propertyInformation. Name. ToLower ());
JsonWriter. WriteValue (valueAsString. Trim ());
}
Catch {}
}
JsonWriter. WriteEndObject ();
JsonWriter. WriteEnd ();
JsonWriter. WriteEndObject ();
}
}
Public void Builder ()
{
Using (jsonWriter = new JsonTextWriter (sw ))
{
JsonWriter. Formatting = Formatting. None;
JsonWriter. Indentation = 4;
JsonWriter. WriteStartObject ();
JsonWriter. WritePropertyName ("member ");
JsonWriter. WriteStartArray ();
JsonWriter. WriteStartObject ();
Type settingsType = this. member. GetType ();
Foreach (PropertyInfo propertyInformation in settingsType. GetProperties ())
{
Try
{
// Skip the DenyReflectionAttrubite attribute here
If (propertyInformation. GetCustomAttributes (typeof (DenyReflectionAttrubite), false). Length> 0) continue;
Object propertyValue = propertyInformation. GetValue (member, null );
String valueAsString = propertyValue. ToString ();
If (propertyValue. Equals (null ))
{
ValueAsString = String. Empty;
}
If (propertyValue. Equals (Int32.MinValue ))
{
ValueAsString = String. Empty;
}
If (propertyValue. Equals (Single. MinValue ))
{
ValueAsString = String. Empty;
}
JsonWriter. WritePropertyName (propertyInformation. Name. ToLower ());
JsonWriter. WriteValue (valueAsString. Trim ());
}
Catch {}
}
JsonWriter. WriteEndObject ();
JsonWriter. WriteEnd ();
JsonWriter. WriteEndObject ();
}
}
Reprinted from: http://www.aspnetjia.com