Beispiel #1
0
		public override void SetValue(object o, string name, object value)
		{
			if (!_scalar)
			{
				// Do not Json null values until it's an array
				//
				if (value == null || (value is XmlNode && IsEmptyNode((XmlNode)value)))
					return;

				if (_first)
					_first = false;
				else
					_sb
						.Append(',')
						.AppendLine()
						;

				for (int i = 0; i < _indent; ++i)
					_sb.Append(' ');

				_sb
					.Append('"')
					.Append(name)
					.Append("\":")
					;
			}

			if (value == null)
				_sb.Append("null");
			else
			{
				switch (Type.GetTypeCode(value.GetType()))
				{
					case TypeCode.Empty:
					case TypeCode.DBNull:
						_sb.Append("null");
						break;
					case TypeCode.Boolean:
						_sb.Append((bool)value? "true": "false");
						break;
					case TypeCode.Char:
						_sb
							.Append('\'')
							.Append((char)value)
							.Append('\'')
							;
						break;
					case TypeCode.SByte:
					case TypeCode.Int16:
					case TypeCode.Int32:
					case TypeCode.Int64:
					case TypeCode.Byte:
					case TypeCode.UInt16:
					case TypeCode.UInt32:
					case TypeCode.UInt64:
					case TypeCode.Single:
					case TypeCode.Double:
					case TypeCode.Decimal:
						_sb.Append(((IFormattable)value).ToString(null, CultureInfo.InvariantCulture));
						break;
					case TypeCode.DateTime:
						_sb
							.Append("new Date(")
							.Append((((DateTime)value).Ticks - InitialJavaScriptDateTicks)/10000)
							.Append(")");
						break;
					case TypeCode.String:
						_sb
							.Append('"')
							.Append(encode((string)value))
							.Append('"')
							;
						break;
					default:
						if (value is XmlNode)
						{
							if (IsEmptyNode((XmlNode) value))
								_sb.Append("null");
							else
								WriteXmlJson((XmlNode)value);
						}
						else
						{
							JsonMapper inner = new JsonMapper(_sb, _indent + 1);

							if (value.GetType().IsArray)
								_mappingSchema.MapSourceListToDestinationList(
									_mappingSchema.GetDataSourceList(value), inner);
							else
								_mappingSchema.MapSourceToDestination(
									_mappingSchema.GetDataSource(value), value, inner, inner);
						}
						break;
				}
			}
		}
Beispiel #2
0
		public void Test()
		{
			JsonMapper jm = new JsonMapper(new StringBuilder(256));

			Map./*[a]*/MapSourceToDestination/*[/a]*/(Map.GetObjectMapper(typeof(SourceObject)), new SourceObject(), jm, jm);
			Console.Write(jm.ToString());

			// Expected output:
			//
			// {
			// "Foo":"Foo",
			// "Bar":1.23,
			// "Baz":new Date(11823840000000000),
			// "Inner":{ "Name":"inner \"object \r name"},
			// "Inner2":
			//  {
			//  "Name":null,
			//  "Value":0
			//  },
			//  "StrArray":["One","Two","Three"]
			// }
		}