Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally support DataContract in Mono/Unity #61

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/SimpleJson/SimpleJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
// NOTE: uncomment the following line to enable DataContract support.
//#define SIMPLE_JSON_DATACONTRACT

// NOTE: uncomment the following line to use alternate definitions of DataContract/DataMember/IgnoreDataMember.
// define if you want to use DataContract with Unity/Mono, which does not implement all WCF classes.
//#define SIMPLE_JSON_REDEFINE_DATACONTRACT_ATTRIBUTES

// NOTE: uncomment the following line to enable IReadOnlyCollection<T> and IReadOnlyList<T> support.
//#define SIMPLE_JSON_READONLY_COLLECTIONS

Expand Down Expand Up @@ -486,6 +490,57 @@ public override IEnumerable<string> GetDynamicMemberNames()

namespace SimpleJson
{
#region Alternate DataContract for Unity/Mono

#if SIMPLE_JSON_REDEFINE_DATACONTRACT_ATTRIBUTES
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum, Inherited = false, AllowMultiple = false)]
#if SIMPLE_JSON_INTERNAL
internal
#else
public
#endif
sealed class DataContractAttribute : Attribute
{
public string Name { get; set; }
}

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, Inherited = false, AllowMultiple = false)]
#if SIMPLE_JSON_INTERNAL
internal
#else
public
#endif
sealed class DataMemberAttribute : Attribute
{
public DataMemberAttribute()
{
}

/// <summary>
/// this constructor simplifies declaring this attribute by writing [DataMember("name")] instead of [DataMember(Name = "name")]
/// </summary>
/// <param name="name"></param>
public DataMemberAttribute(string name)
{
Name = name;
}

public string Name { get; set; }
}

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, Inherited = false, AllowMultiple = false)]
#if SIMPLE_JSON_INTERNAL
internal
#else
public
#endif
sealed class IgnoreDataMemberAttribute : Attribute
{
}
#endif

#endregion

/// <summary>
/// This class encodes and decodes JSON strings.
/// Spec. details, see http://www.json.org/
Expand Down