The Newtonsoft. Json component converts object2json to newtonsoft. json.

Source: Internet
Author: User

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

Related Article

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.