ln.json/ln.json/mapping/JSONArrayMapping.cs

46 lines
1.2 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;
namespace ln.json.mapping
{
public class JSONArrayMapping : JSONMapping
{
Type elementType;
public JSONArrayMapping(Type type)
:base(type)
{
elementType = type.GetElementType();
}
public override object FromJson(JSONMapper mapper, JSONValue json)
{
JSONArray jsonArray = (JSONArray)json;
Array array = Array.CreateInstance(elementType,jsonArray.Count);
for (int n = 0; n < array.Length; n++)
array.SetValue(mapper.FromJson(jsonArray[n],elementType),n);
return array;
}
public override JSONValue ToJson(JSONMapper mapper, object value)
{
Array array = (Array)value;
JSONArray jsonArray = new JSONArray();
foreach (object element in array)
jsonArray.Add(mapper.ToJson(element));
return jsonArray;
}
}
}