ln.json/mapping/JSONEnumerableMapping.cs

64 lines
1.7 KiB
C#

// /**
// * File: JSONArrayMapping.cs
// * Author: haraldwolff
// *
// * This file and it's content is copyrighted by the Author and / or copyright holder.
// * Any use wihtout proper permission is illegal and may lead to legal actions.
// *
// *
// **/
using System;
using System.Collections.Generic;
using System.Collections;
namespace ln.json.mapping
{
public class JSONEnumerableMapping<T> : JSONMapping
{
Type elementType;
public JSONEnumerableMapping()
: base(typeof(IEnumerable<T>))
{
elementType = typeof(T);
}
public override object FromJson(JSONMapper mapper, JSONValue json)
{
throw new NotSupportedException("IEnumerable<> can not be set from JSON");
}
public override JSONValue ToJson(JSONMapper mapper, object value)
{
IEnumerable<T> ie = (IEnumerable<T>)value;
JSONArray jsonArray = new JSONArray();
foreach (object element in ie)
jsonArray.Add(mapper.ToJson(element));
return jsonArray;
}
}
public class JSONEnumerableMapping : JSONMapping
{
public JSONEnumerableMapping()
: base(typeof(IEnumerable))
{
}
public override object FromJson(JSONMapper mapper, JSONValue json)
{
throw new NotSupportedException("IEnumerable can not be set from JSON");
}
public override JSONValue ToJson(JSONMapper mapper, object value)
{
IEnumerable ie = (IEnumerable)value;
JSONArray jsonArray = new JSONArray();
foreach (object element in ie)
jsonArray.Add(mapper.ToJson(element));
return jsonArray;
}
}
}