1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
|
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
#if FEATURE_DYNAMIC
using System.Dynamic;
#endif
using System.Globalization;
using System.IO;
using System.Linq.Expressions;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Xml;
namespace System.Json
{
/// <summary>
/// This is the base class for JavaScript Object Notation (JSON) common language runtime (CLR) types.
/// </summary>
[SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix",
Justification = "JsonValue is by definition either a collection or a single object.")]
[DataContract]
#if FEATURE_DYNAMIC
public class JsonValue : IEnumerable<KeyValuePair<string, JsonValue>>, IDynamicMetaObjectProvider
#else
public class JsonValue : IEnumerable<KeyValuePair<string, JsonValue>>
#endif
{
private static JsonValue defaultInstance = new JsonValue();
private int changingListenersCount;
private int changedListenersCount;
internal JsonValue()
{
}
/// <summary>
/// Raised when this <see cref="System.Json.JsonValue"/> or any of its members have changed.
/// </summary>
/// <remarks><p>Events are raised when elements are added or removed to <see cref="System.Json.JsonValue"/>
/// instances. It applies to both complex descendants of <see cref="System.Json.JsonValue"/>: <see cref="System.Json.JsonArray"/>
/// and <see cref="System.Json.JsonObject"/>.</p>
/// <p>You should be careful when modifying a <see cref="System.Json.JsonValue"/> tree within one of these events,
/// because doing this might lead to unexpected results. For example, if you receive a Changing event, and while
/// the event is being processed you remove the node from the tree, you might not receive the Changed event. When
/// an event is being processed, it is valid to modify a tree other than the one that contains the node that is
/// receiving the event; it is even valid to modify the same tree provided the modifications do not affect the
/// specific nodes on which the event was raised. However, if you modify the area of the tree that contains the
/// node receiving the event, the events that you receive and the impact to the tree are undefined.</p></remarks>
public event EventHandler<JsonValueChangeEventArgs> Changed
{
add
{
changedListenersCount++;
OnChanged += value;
}
remove
{
changedListenersCount--;
OnChanged -= value;
}
}
/// <summary>
/// Raised when this <see cref="System.Json.JsonValue"/> or any of its members are about to be changed.
/// </summary>
/// <remarks><p>Events are raised when elements are added or removed to <see cref="System.Json.JsonValue"/>
/// instances. It applies to both complex descendants of <see cref="System.Json.JsonValue"/>: <see cref="System.Json.JsonArray"/>
/// and <see cref="System.Json.JsonObject"/>.</p>
/// <p>You should be careful when modifying a <see cref="System.Json.JsonValue"/> tree within one of these events,
/// because doing this might lead to unexpected results. For example, if you receive a Changing event, and while
/// the event is being processed you remove the node from the tree, you might not receive the Changed event. When
/// an event is being processed, it is valid to modify a tree other than the one that contains the node that is
/// receiving the event; it is even valid to modify the same tree provided the modifications do not affect the
/// specific nodes on which the event was raised. However, if you modify the area of the tree that contains the
/// node receiving the event, the events that you receive and the impact to the tree are undefined.</p></remarks>
public event EventHandler<JsonValueChangeEventArgs> Changing
{
add
{
changingListenersCount++;
OnChanging += value;
}
remove
{
changingListenersCount--;
OnChanging -= value;
}
}
private event EventHandler<JsonValueChangeEventArgs> OnChanged;
private event EventHandler<JsonValueChangeEventArgs> OnChanging;
/// <summary>
/// Gets the JSON CLR type represented by this instance.
/// </summary>
public virtual JsonType JsonType
{
get { return JsonType.Default; }
}
/// <summary>
/// Gets the number of items in this object.
/// </summary>
public virtual int Count
{
get { return 0; }
}
/// <summary>
/// Gets the number of listeners to the <see cref="Changing"/> event for this instance.
/// </summary>
protected int ChangingListenersCount
{
get { return changingListenersCount; }
}
/// <summary>
/// Gets the number of listeners to the <see cref="Changed"/> event for this instance.
/// </summary>
protected int ChangedListenersCount
{
get { return changedListenersCount; }
}
/// <summary>
/// Gets the default JsonValue instance.
/// This instance enables safe-chaining of JsonValue operations and resolves to 'null'
/// when this instance is used as dynamic, mapping to the JavaScript 'null' value.
/// </summary>
private static JsonValue DefaultInstance
{
get { return defaultInstance; }
}
/// <summary>
/// This indexer is not supported for this base class and throws an exception.
/// </summary>
/// <param name="key">The key of the element to get or set.</param>
/// <returns><see cref="System.Json.JsonValue"/>.</returns>
/// <remarks>The exception thrown is the <see cref="System.InvalidOperationException"/>.
/// This method is overloaded in the implementation of the <see cref="System.Json.JsonObject"/>
/// class, which inherits from this class.</remarks>
public virtual JsonValue this[string key]
{
get { throw new InvalidOperationException(RS.Format(Properties.Resources.IndexerNotSupportedOnJsonType, typeof(string), JsonType)); }
set { throw new InvalidOperationException(RS.Format(Properties.Resources.IndexerNotSupportedOnJsonType, typeof(string), JsonType)); }
}
/// <summary>
/// This indexer is not supported for this base class and throws an exception.
/// </summary>
/// <param name="index">The zero-based index of the element to get or set.</param>
/// <returns><see cref="System.Json.JsonValue"/>.</returns>
/// <remarks>The exception thrown is the <see cref="System.InvalidOperationException"/>.
/// This method is overloaded in the implementation of the <see cref="System.Json.JsonArray"/>
/// class, which inherits from this class.</remarks>
public virtual JsonValue this[int index]
{
get { throw new InvalidOperationException(RS.Format(Properties.Resources.IndexerNotSupportedOnJsonType, typeof(int), JsonType)); }
set { throw new InvalidOperationException(RS.Format(Properties.Resources.IndexerNotSupportedOnJsonType, typeof(int), JsonType)); }
}
/// <summary>
/// Deserializes text-based JSON into a JSON CLR type.
/// </summary>
/// <param name="json">The text-based JSON to be parsed into a JSON CLR type.</param>
/// <returns>The <see cref="System.Json.JsonValue"/> object that represents the parsed
/// text-based JSON as a CLR type.</returns>
/// <exception cref="System.ArgumentException">The length of jsonString is zero.</exception>
/// <exception cref="System.ArgumentNullException">jsonString is null.</exception>
/// <remarks>The result will be an instance of either <see cref="System.Json.JsonArray"/>,
/// <see cref="System.Json.JsonObject"/> or <see cref="System.Json.JsonPrimitive"/>,
/// depending on the text-based JSON supplied to the method.</remarks>
public static JsonValue Parse(string json)
{
return JXmlToJsonValueConverter.JXMLToJsonValue(json);
}
/// <summary>
/// Deserializes text-based JSON from a text reader into a JSON CLR type.
/// </summary>
/// <param name="textReader">A <see cref="System.IO.TextReader"/> over text-based JSON content.</param>
/// <returns>The <see cref="System.Json.JsonValue"/> object that represents the parsed
/// text-based JSON as a CLR type.</returns>
/// <exception cref="System.ArgumentNullException">textReader is null.</exception>
/// <remarks>The result will be an instance of either <see cref="System.Json.JsonArray"/>,
/// <see cref="System.Json.JsonObject"/> or <see cref="System.Json.JsonPrimitive"/>,
/// depending on the text-based JSON supplied to the method.</remarks>
public static JsonValue Load(TextReader textReader)
{
if (textReader == null)
{
throw new ArgumentNullException("textReader");
}
return JsonValue.Parse(textReader.ReadToEnd());
}
/// <summary>
/// Deserializes text-based JSON from a stream into a JSON CLR type.
/// </summary>
/// <param name="stream">A <see cref="System.IO.Stream"/> that contains text-based JSON content.</param>
/// <returns>The <see cref="System.Json.JsonValue"/> object that represents the parsed
/// text-based JSON as a CLR type.</returns>
/// <exception cref="System.ArgumentNullException">stream is null.</exception>
/// <remarks>The result will be an instance of either <see cref="System.Json.JsonArray"/>,
/// <see cref="System.Json.JsonObject"/> or <see cref="System.Json.JsonPrimitive"/>,
/// depending on the text-based JSON supplied to the method.</remarks>
public static JsonValue Load(Stream stream)
{
return JXmlToJsonValueConverter.JXMLToJsonValue(stream);
}
/// <summary>
/// Performs a cast operation from a <see cref="JsonValue"/> instance into the specified type parameter./>
/// </summary>
/// <typeparam name="T">The type to cast the instance to.</typeparam>
/// <param name="value">The <see cref="System.Json.JsonValue"/> instance.</param>
/// <returns>An object of type T initialized with the <see cref="System.Json.JsonValue"/> value specified.</returns>
/// <remarks>This method is to support the framework and is not intended to be used externally, use explicit type cast instead.</remarks>
[EditorBrowsable(EditorBrowsableState.Never)]
[SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter",
Justification = "The generic parameter is used to specify the output type")]
public static T CastValue<T>(JsonValue value)
{
Type typeofT = typeof(T);
if ((value != null && typeofT.IsAssignableFrom(value.GetType())) || typeofT == typeof(object))
{
return (T)(object)value;
}
if (value == null || value.JsonType == JsonType.Default)
{
if (typeofT.IsValueType)
{
throw new InvalidCastException(RS.Format(Properties.Resources.InvalidCastNonNullable, typeofT.FullName));
}
else
{
return default(T);
}
}
try
{
return value.ReadAs<T>();
}
catch (Exception ex)
{
if (ex is FormatException || ex is NotSupportedException || ex is InvalidCastException)
{
throw new InvalidCastException(RS.Format(Properties.Resources.CannotCastJsonValue, value.GetType().FullName, typeofT.FullName), ex);
}
throw;
}
}
/// <summary>
/// Returns an enumerator which iterates through the values in this object.
/// </summary>
/// <returns>An enumerator which which iterates through the values in this object.</returns>
/// <remarks>The enumerator returned by this class is empty; subclasses will override this method to return appropriate enumerators for themselves.</remarks>
public IEnumerator<KeyValuePair<string, JsonValue>> GetEnumerator()
{
return GetKeyValuePairEnumerator();
}
/// <summary>
/// Returns an enumerator which iterates through the values in this object.
/// </summary>
/// <returns>An <see cref="System.Collections.IEnumerator"/> which iterates through the values in this object.</returns>
/// <remarks>The enumerator returned by this class is empty; subclasses will override this method to return appropriate enumerators for themselves.</remarks>
IEnumerator IEnumerable.GetEnumerator()
{
return GetKeyValuePairEnumerator();
}
#if FEATURE_DYNAMIC
/// <summary>
/// Gets this instance as a <code>dynamic</code> object.
/// </summary>
/// <returns>This instance as <code>dynamic</code>.</returns>
public dynamic AsDynamic()
{
return this;
}
#endif
/// <summary>
/// Attempts to convert this <see cref="System.Json.JsonValue"/> instance into the type T.
/// </summary>
/// <typeparam name="T">The type to which the conversion is being performed.</typeparam>
/// <param name="valueOfT">An instance of T initialized with this instance, or the default value of T if the conversion cannot be performed.</param>
/// <returns>true if this <see cref="System.Json.JsonValue"/> instance can be read as type T; otherwise, false.</returns>
public bool TryReadAs<T>(out T valueOfT)
{
object value;
if (TryReadAs(typeof(T), out value))
{
valueOfT = (T)value;
return true;
}
valueOfT = default(T);
return false;
}
/// <summary>
/// Attempts to convert this <see cref="System.Json.JsonValue"/> instance into the type T.
/// </summary>
/// <typeparam name="T">The type to which the conversion is being performed.</typeparam>
/// <returns>An instance of T initialized with the value from the conversion of this instance.</returns>
/// <exception cref="System.NotSupportedException">If this <see cref="System.Json.JsonValue"/> value cannot be converted into the type T.</exception>
[SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter",
Justification = "The generic parameter is used to specify the output type")]
public T ReadAs<T>()
{
return (T)ReadAs(typeof(T));
}
/// <summary>
/// Attempts to convert this <see cref="System.Json.JsonValue"/> instance into the type T.
/// </summary>
/// <typeparam name="T">The type to which the conversion is being performed.</typeparam>
/// <param name="fallback">The fallback value to be returned if the conversion cannot be made.</param>
/// <returns>An instance of T initialized with the value from the conversion of this instance, or the specified fallback value if the conversion cannot be made.</returns>
public T ReadAs<T>(T fallback)
{
return (T)ReadAs(typeof(T), fallback);
}
/// <summary>
/// Attempts to convert this <see cref="System.Json.JsonValue"/> instance to an instance of the specified type.
/// </summary>
/// <param name="type">The type to which the conversion is being performed.</param>
/// <param name="fallback">The fallback value to be returned if the conversion cannot be made.</param>
/// <returns>An instance of the specified type initialized with the value from the conversion of this instance, or the specified fallback value if the conversion cannot be made.</returns>
public object ReadAs(Type type, object fallback)
{
if (type == null)
{
throw new ArgumentNullException("type");
}
object result;
if (JsonType != JsonType.Default && TryReadAs(type, out result))
{
return result;
}
else
{
return fallback;
}
}
/// <summary>
/// Attempts to convert this <see cref="System.Json.JsonValue"/> instance into an instance of the specified type.
/// </summary>
/// <param name="type">The type to which the conversion is being performed.</param>
/// <returns>An instance of the specified type initialized with the value from the conversion of this instance.</returns>
/// <exception cref="System.NotSupportedException">If this <see cref="System.Json.JsonValue"/> value cannot be converted into the type T.</exception>
public virtual object ReadAs(Type type)
{
if (type == null)
{
throw new ArgumentNullException("type");
}
object result;
if (TryReadAs(type, out result))
{
return result;
}
throw new NotSupportedException(RS.Format(Properties.Resources.CannotReadAsType, GetType().FullName, type.FullName));
}
/// <summary>
/// Attempts to convert this <see cref="System.Json.JsonValue"/> instance into an instance of the specified type.
/// </summary>
/// <param name="type">The type to which the conversion is being performed.</param>
/// <param name="value">An object to be initialized with this instance or null if the conversion cannot be performed.</param>
/// <returns>true if this <see cref="System.Json.JsonValue"/> instance can be read as the specified type; otherwise, false.</returns>
[SuppressMessage("Microsoft.Design", "CA1007:UseGenericsWhereAppropriate",
Justification = "This is the non-generic version of the method.")]
public virtual bool TryReadAs(Type type, out object value)
{
if (type == null)
{
throw new ArgumentNullException("type");
}
if (type.IsAssignableFrom(GetType()) || type == typeof(object))
{
value = this;
return true;
}
value = null;
return false;
}
/// <summary>
/// Writes this <see cref="System.Json.JsonValue"/> instance to a <see cref="System.IO.Stream"/>.
/// </summary>
/// <param name="stream">Stream to which to write text-based JSON.</param>
public void Save(Stream stream)
{
if (JsonType == JsonType.Default)
{
throw new InvalidOperationException(Properties.Resources.UseOfDefaultNotAllowed);
}
if (stream == null)
{
throw new ArgumentNullException("stream");
}
using (XmlDictionaryWriter jsonWriter = JsonReaderWriterFactory.CreateJsonWriter(stream, Encoding.UTF8, false))
{
jsonWriter.WriteStartElement(JXmlToJsonValueConverter.RootElementName);
Save(jsonWriter);
jsonWriter.WriteEndElement();
}
}
/// <summary>
/// Writes <see cref="System.Json.JsonValue"/> instance to a <see cref="TextWriter"/>.
/// </summary>
/// <param name="textWriter">The <see cref="System.IO.TextWriter"/> used to write text-based JSON.</param>
public void Save(TextWriter textWriter)
{
if (JsonType == JsonType.Default)
{
throw new InvalidOperationException(Properties.Resources.UseOfDefaultNotAllowed);
}
if (textWriter == null)
{
throw new ArgumentNullException("textWriter");
}
using (MemoryStream ms = new MemoryStream())
{
Save(ms);
ms.Position = 0;
textWriter.Write(new StreamReader(ms).ReadToEnd());
}
}
/// <summary>
/// Provides a textual representation of this <see cref="System.Json.JsonValue"/> instance.
/// </summary>
/// <returns>A <see cref="System.String"/> containing text-based JSON.</returns>
public override string ToString()
{
if (JsonType == JsonType.Default)
{
return "Default";
}
using (MemoryStream ms = new MemoryStream())
{
Save(ms);
ms.Position = 0;
return new StreamReader(ms).ReadToEnd();
}
}
/// <summary>
/// Checks whether a key/value pair with a specified key exists in the JSON CLR object type.
/// </summary>
/// <param name="key">The key to check for.</param>
/// <returns>false in this class; subclasses may override this method to return other values.</returns>
/// <remarks>This method is overloaded in the implementation of the <see cref="System.Json.JsonObject"/>
/// class, which inherits from this class.</remarks>
public virtual bool ContainsKey(string key)
{
return false;
}
/// <summary>
/// Returns the value returned by the safe string indexer for this instance.
/// </summary>
/// <param name="key">The key of the element to get.</param>
/// <returns>If this is an instance of <see cref="System.Json.JsonObject"/>, it contains
/// the given key and the value corresponding to the key is not null, then it will return that value.
/// Otherwise it will return a <see cref="System.Json.JsonValue"/> instance with <see cref="System.Json.JsonValue.JsonType"/>
/// equals to <see cref="F:System.Json.JsonType.Default"/>.</returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public virtual JsonValue GetValue(string key)
{
return ValueOrDefault(key);
}
/// <summary>
/// Returns the value returned by the safe int indexer for this instance.
/// </summary>
/// <param name="index">The zero-based index of the element to get.</param>
/// <returns>If this is an instance of <see cref="System.Json.JsonArray"/>, the index is within the array
/// bounds, and the value corresponding to the index is not null, then it will return that value.
/// Otherwise it will return a <see cref="System.Json.JsonValue"/> instance with <see cref="System.Json.JsonValue.JsonType"/>
/// equals to <see cref="F:System.Json.JsonType.Default"/>.</returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public virtual JsonValue GetValue(int index)
{
return ValueOrDefault(index);
}
/// <summary>
/// Sets the value and returns it.
/// </summary>
/// <param name="key">The key of the element to set.</param>
/// <param name="value">The value to be set.</param>
/// <returns>The value, converted into a JsonValue, set in this collection.</returns>
/// <exception cref="System.ArgumentException">If the value cannot be converted into a JsonValue.</exception>
[EditorBrowsable(EditorBrowsableState.Never)]
public virtual JsonValue SetValue(string key, object value)
{
this[key] = ResolveObject(value);
return this[key];
}
/// <summary>
/// Sets the value and returns it.
/// </summary>
/// <param name="index">The zero-based index of the element to set.</param>
/// <param name="value">The value to be set.</param>
/// <returns>The value, converted into a JsonValue, set in this collection.</returns>
/// <exception cref="System.ArgumentException">If the value cannot be converted into a JsonValue.</exception>
[EditorBrowsable(EditorBrowsableState.Never)]
public virtual JsonValue SetValue(int index, object value)
{
this[index] = ResolveObject(value);
return this[index];
}
/// <summary>
/// Safe string indexer for the <see cref="System.Json.JsonValue"/> type.
/// </summary>
/// <param name="key">The key of the element to get.</param>
/// <returns>If this is an instance of <see cref="System.Json.JsonObject"/>, it contains
/// the given key and the value corresponding to the key is not null, then it will return that value.
/// Otherwise it will return a <see cref="System.Json.JsonValue"/> instance with <see cref="System.Json.JsonValue.JsonType"/>
/// equals to <see cref="F:System.Json.JsonType.Default"/>.</returns>
public virtual JsonValue ValueOrDefault(string key)
{
return JsonValue.DefaultInstance;
}
/// <summary>
/// Safe indexer for the <see cref="System.Json.JsonValue"/> type.
/// </summary>
/// <param name="index">The zero-based index of the element to get.</param>
/// <returns>If this is an instance of <see cref="System.Json.JsonArray"/>, the index is within the array
/// bounds, and the value corresponding to the index is not null, then it will return that value.
/// Otherwise it will return a <see cref="System.Json.JsonValue"/> instance with <see cref="System.Json.JsonValue.JsonType"/>
/// equals to <see cref="F:System.Json.JsonType.Default"/>.</returns>
public virtual JsonValue ValueOrDefault(int index)
{
return JsonValue.DefaultInstance;
}
/// <summary>
/// Safe deep indexer for the <see cref="JsonValue"/> type.
/// </summary>
/// <param name="indexes">The indices to index this type. The indices can be
/// of type <see cref="System.Int32"/> or <see cref="System.String"/>.</param>
/// <returns>A <see cref="JsonValue"/> which is equivalent to calling<see cref="ValueOrDefault(int)"/> or
/// <see cref="ValueOrDefault(string)"/> on the first index, then calling it again on the result
/// for the second index and so on.</returns>
/// <exception cref="System.ArgumentException">If any of the indices is not of type
/// <see cref="System.Int32"/> or <see cref="System.String"/>.</exception>
public JsonValue ValueOrDefault(params object[] indexes)
{
if (indexes == null)
{
return JsonValue.DefaultInstance;
}
if (indexes.Length == 0)
{
return this;
}
JsonValue result = this;
for (int i = 0; i < indexes.Length; i++)
{
object index = indexes[i];
if (index == null)
{
result = JsonValue.DefaultInstance;
continue;
}
Type indexType = index.GetType();
switch (Type.GetTypeCode(indexType))
{
case TypeCode.Char:
case TypeCode.Int16:
case TypeCode.UInt16:
case TypeCode.Byte:
case TypeCode.SByte:
index = System.Convert.ChangeType(index, typeof(int), CultureInfo.InvariantCulture);
goto case TypeCode.Int32;
case TypeCode.Int32:
result = result.ValueOrDefault((int)index);
break;
case TypeCode.String:
result = result.ValueOrDefault((string)index);
break;
default:
throw new ArgumentException(RS.Format(Properties.Resources.InvalidIndexType, index.GetType()), "indexes");
}
}
return result;
}
#if FEATURE_DYNAMIC
[SuppressMessage("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes",
Justification = "Cannot make this class sealed, it needs to have subclasses. But its subclasses are sealed themselves.")]
DynamicMetaObject IDynamicMetaObjectProvider.GetMetaObject(Expression parameter)
{
if (parameter == null)
{
throw new ArgumentNullException("parameter");
}
return new JsonValueDynamicMetaObject(parameter, this);
}
#endif
/// <summary>
/// Resolves the specified object to an appropriate JsonValue instance.
/// </summary>
/// <param name="value">The object to resolve.</param>
/// <returns>A <see cref="JsonValue"/> instance resolved from the specified object.</returns>
internal static JsonValue ResolveObject(object value)
{
JsonPrimitive primitive;
if (value == null)
{
return null;
}
JsonValue jsonValue = value as JsonValue;
if (jsonValue != null)
{
return jsonValue;
}
if (JsonPrimitive.TryCreate(value, out primitive))
{
return primitive;
}
throw new ArgumentException(Properties.Resources.TypeNotSupported, "value");
}
/// <summary>
/// Determines whether an explicit cast to JsonValue is provided from the specified type.
/// </summary>
/// <param name="type">The type to check.</param>
/// <returns>true if an explicit cast exists for the specified type, false otherwise.</returns>
internal static bool IsSupportedExplicitCastType(Type type)
{
TypeCode typeCode = Type.GetTypeCode(type);
switch (typeCode)
{
case TypeCode.Boolean:
case TypeCode.Byte:
case TypeCode.Char:
case TypeCode.DateTime:
case TypeCode.Decimal:
case TypeCode.Double:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.SByte:
case TypeCode.Single:
case TypeCode.String:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
return true;
default:
return type == typeof(DateTimeOffset) || type == typeof(Guid) || type == typeof(Uri) ||
type == typeof(List<object>) || type == typeof(Array) || type == typeof(object[]) ||
type == typeof(Dictionary<string, object>);
}
}
/// <summary>
/// Returns the value this object wraps (if any).
/// </summary>
/// <returns>The value wrapped by this instance or null if none.</returns>
internal virtual object Read()
{
return null;
}
/// <summary>
/// Serializes this object into the specified <see cref="XmlDictionaryWriter"/> instance.
/// </summary>
/// <param name="jsonWriter">An <see cref="XmlDictionaryWriter"/> instance to serialize this instance into.</param>
internal virtual void Save(XmlDictionaryWriter jsonWriter)
{
if (jsonWriter == null)
{
throw new ArgumentNullException("jsonWriter");
}
Stack<JsonValue> objectStack = new Stack<JsonValue>();
Stack<int> indexStack = new Stack<int>();
int currentIndex = 0;
JsonValue currentValue = this;
OnSaveStarted();
WriteAttributeString(jsonWriter);
while (currentIndex < currentValue.Count || objectStack.Count > 0)
{
if (currentValue.Count > currentIndex)
{
JsonValue nextValue = currentValue.WriteStartElementAndGetNext(jsonWriter, currentIndex);
if (JsonValue.IsJsonCollection(nextValue))
{
nextValue.OnSaveStarted();
nextValue.WriteAttributeString(jsonWriter);
objectStack.Push(currentValue);
indexStack.Push(currentIndex);
currentValue = nextValue;
currentIndex = 0;
}
else
{
if (nextValue == null)
{
jsonWriter.WriteAttributeString(JXmlToJsonValueConverter.TypeAttributeName, JXmlToJsonValueConverter.NullAttributeValue);
}
else
{
nextValue.Save(jsonWriter);
}
currentIndex++;
jsonWriter.WriteEndElement();
}
}
else
{
if (objectStack.Count > 0)
{
currentValue.OnSaveEnded();
jsonWriter.WriteEndElement();
currentValue = objectStack.Pop();
currentIndex = indexStack.Pop() + 1;
}
}
}
OnSaveEnded();
}
/// <summary>
/// Returns an enumerator which iterates through the values in this object.
/// </summary>
/// <returns>An <see cref="System.Collections.IEnumerator"/> which iterates through the values in this object.</returns>
/// <remarks>This method is the virtual version of the IEnumerator.GetEnumerator method and is provided to allow derived classes to implement the
/// appropriate version of the generic interface (enumerator of values or key/value pairs).</remarks>
[SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate",
Justification = "This method is a virtual version of the IEnumerable.GetEnumerator method.")]
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures",
Justification = "This class is a collection that is properly represented by the nested generic type.")]
protected virtual IEnumerator<KeyValuePair<string, JsonValue>> GetKeyValuePairEnumerator()
{
yield break;
}
/// <summary>
/// Callback method called during Save operations to let the instance write the start element
/// and return the next element in the collection.
/// </summary>
/// <param name="jsonWriter">The JXML writer used to write JSON.</param>
/// <param name="index">The index within this collection.</param>
/// <returns>The next item in the collection, or null of there are no more items.</returns>
internal virtual JsonValue WriteStartElementAndGetNext(XmlDictionaryWriter jsonWriter, int index)
{
return null;
}
/// <summary>
/// Callback method called to let an instance write the proper JXML attribute when saving this
/// instance.
/// </summary>
/// <param name="jsonWriter">The JXML writer used to write JSON.</param>
internal virtual void WriteAttributeString(XmlDictionaryWriter jsonWriter)
{
}
/// <summary>
/// Callback method called when a Save operation is starting for this instance.
/// </summary>
protected virtual void OnSaveStarted()
{
}
/// <summary>
/// Callback method called when a Save operation is finished for this instance.
/// </summary>
protected virtual void OnSaveEnded()
{
}
/// <summary>
/// Called internally to raise the <see cref="Changing"/> event.
/// </summary>
/// <param name="sender">The object which caused the event to be raised.</param>
/// <param name="eventArgs">The arguments to the event.</param>
[SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate",
Justification = "This is a helper function used to raise the event.")]
[SuppressMessage("Microsoft.Security", "CA2109:ReviewVisibleEventHandlers",
Justification = "This is not externally visible, since the constructor for this class is internal (cannot be directly derived) and all its subclasses are sealed.")]
protected void RaiseChangingEvent(object sender, JsonValueChangeEventArgs eventArgs)
{
EventHandler<JsonValueChangeEventArgs> changing = OnChanging;
if (changing != null)
{
changing(sender, eventArgs);
}
}
/// <summary>
/// Called internally to raise the <see cref="Changed"/> event.
/// </summary>
/// <param name="sender">The object which caused the event to be raised.</param>
/// <param name="eventArgs">The arguments to the event.</param>
[SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate",
Justification = "This is a helper function used to raise the event.")]
[SuppressMessage("Microsoft.Security", "CA2109:ReviewVisibleEventHandlers",
Justification = "This is not externally visible, since the constructor for this class is internal (cannot be directly derived) and all its subclasses are sealed.")]
protected void RaiseChangedEvent(object sender, JsonValueChangeEventArgs eventArgs)
{
EventHandler<JsonValueChangeEventArgs> changed = OnChanged;
if (changed != null)
{
changed(sender, eventArgs);
}
}
private static bool IsJsonCollection(JsonValue value)
{
return value != null && (value.JsonType == JsonType.Array || value.JsonType == JsonType.Object);
}
/// <summary>
/// Enables explicit casts from an instance of type <see cref="System.Json.JsonValue"/> to a <see cref="System.String"/> object.
/// </summary>
/// <param name="value">The instance of <see cref="System.Json.JsonValue"/> used to initialize the <see cref="System.String"/> object.</param>
/// <returns>The <see cref="System.String"/> initialized with the <see cref="System.Json.JsonValue"/> value specified or null if value is null.</returns>
public static explicit operator string(JsonValue value)
{
return CastValue<string>(value);
}
/// <summary>
/// Enables explicit casts from an instance of type <see cref="System.Json.JsonValue"/> to a <see cref="System.Double"/> object.
/// </summary>
/// <param name="value">The instance of <see cref="System.Json.JsonValue"/> used to initialize the <see cref="System.Double"/> object.</param>
/// <returns>The <see cref="System.Double"/> initialized with the <see cref="System.Json.JsonValue"/> value specified.</returns>
public static explicit operator double(JsonValue value)
{
return CastValue<double>(value);
}
/// <summary>
/// Enables explicit casts from an instance of type <see cref="System.Json.JsonValue"/> to a <see cref="System.Single"/> object.
/// </summary>
/// <param name="value">The instance of <see cref="System.Json.JsonValue"/> used to initialize the <see cref="System.Single"/> object.</param>
/// <returns>The <see cref="System.Single"/> initialized with the <see cref="System.Json.JsonValue"/> value specified.</returns>
public static explicit operator float(JsonValue value)
{
return CastValue<float>(value);
}
/// <summary>
/// Enables explicit casts from an instance of type <see cref="System.Json.JsonValue"/> to a <see cref="System.Decimal"/> object.
/// </summary>
/// <param name="value">The instance of <see cref="System.Json.JsonValue"/> used to initialize the <see cref="System.Decimal"/> object.</param>
/// <returns>The <see cref="System.Decimal"/> initialized with the <see cref="System.Json.JsonValue"/> value specified.</returns>
public static explicit operator decimal(JsonValue value)
{
return CastValue<decimal>(value);
}
/// <summary>
/// Enables explicit casts from an instance of type <see cref="System.Json.JsonValue"/> to a <see cref="System.Int64"/> object.
/// </summary>
/// <param name="value">The instance of <see cref="System.Json.JsonValue"/> used to initialize the <see cref="System.Int64"/> object.</param>
/// <returns>The <see cref="System.Int64"/> initialized with the <see cref="System.Json.JsonValue"/> value specified.</returns>
public static explicit operator long(JsonValue value)
{
return CastValue<long>(value);
}
/// <summary>
/// Enables explicit casts from an instance of type <see cref="System.Json.JsonValue"/> to a <see cref="System.UInt64"/> object.
/// </summary>
/// <param name="value">The instance of <see cref="System.Json.JsonValue"/> used to initialize the <see cref="System.UInt64"/> object.</param>
/// <returns>The <see cref="System.UInt64"/> initialized with the <see cref="System.Json.JsonValue"/> value specified.</returns>
[CLSCompliant(false)]
public static explicit operator ulong(JsonValue value)
{
return CastValue<ulong>(value);
}
/// <summary>
/// Enables explicit casts from an instance of type <see cref="System.Json.JsonValue"/> to a <see cref="System.Int32"/> object.
/// </summary>
/// <param name="value">The instance of <see cref="System.Json.JsonValue"/> used to initialize the <see cref="System.Int32"/> object.</param>
/// <returns>The <see cref="System.Int32"/> initialized with the <see cref="System.Json.JsonValue"/> value specified.</returns>
public static explicit operator int(JsonValue value)
{
return CastValue<int>(value);
}
/// <summary>
/// Enables explicit casts from an instance of type <see cref="System.Json.JsonValue"/> to a <see cref="System.UInt32"/> object.
/// </summary>
/// <param name="value">The instance of <see cref="System.Json.JsonValue"/> used to initialize the <see cref="System.UInt32"/> object.</param>
/// <returns>The <see cref="System.UInt32"/> initialized with the <see cref="System.Json.JsonValue"/> value specified.</returns>
[CLSCompliant(false)]
public static explicit operator uint(JsonValue value)
{
return CastValue<uint>(value);
}
/// <summary>
/// Enables explicit casts from an instance of type <see cref="System.Json.JsonValue"/> to a <see cref="System.Int16"/> object.
/// </summary>
/// <param name="value">The instance of <see cref="System.Json.JsonValue"/> used to initialize the <see cref="System.Int16"/> object.</param>
/// <returns>The <see cref="System.Int16"/> initialized with the <see cref="System.Json.JsonValue"/> value specified.</returns>
public static explicit operator short(JsonValue value)
{
return CastValue<short>(value);
}
/// <summary>
/// Enables explicit casts from an instance of type <see cref="System.Json.JsonValue"/> to a <see cref="System.UInt16"/> object.
/// </summary>
/// <param name="value">The instance of <see cref="System.Json.JsonValue"/> used to initialize the <see cref="System.UInt16"/> object.</param>
/// <returns>The <see cref="System.UInt16"/> initialized with the <see cref="System.Json.JsonValue"/> value specified.</returns>
[CLSCompliant(false)]
public static explicit operator ushort(JsonValue value)
{
return CastValue<ushort>(value);
}
/// <summary>
/// Enables explicit casts from an instance of type <see cref="System.Json.JsonValue"/> to a <see cref="System.SByte"/> object.
/// </summary>
/// <param name="value">The instance of <see cref="System.Json.JsonValue"/> used to initialize the <see cref="System.SByte"/> object.</param>
/// <returns>The <see cref="System.SByte"/> initialized with the <see cref="System.Json.JsonValue"/> value specified.</returns>
[CLSCompliant(false)]
public static explicit operator sbyte(JsonValue value)
{
return CastValue<sbyte>(value);
}
/// <summary>
/// Enables explicit casts from an instance of type <see cref="System.Json.JsonValue"/> to a <see cref="System.Byte"/> object.
/// </summary>
/// <param name="value">The instance of <see cref="System.Json.JsonValue"/> used to initialize the <see cref="System.Byte"/> object.</param>
/// <returns>The <see cref="System.Byte"/> initialized with the <see cref="System.Json.JsonValue"/> value specified.</returns>
public static explicit operator byte(JsonValue value)
{
return CastValue<byte>(value);
}
/// <summary>
/// Enables explicit casts from an instance of type <see cref="System.Json.JsonValue"/> to a <see cref="System.Uri"/> object.
/// </summary>
/// <param name="value">The instance of <see cref="System.Json.JsonValue"/> used to initialize the <see cref="System.Uri"/> object.</param>
/// <returns>The <see cref="System.Uri"/> initialized with the <see cref="System.Json.JsonValue"/> value specified or null if value is null.</returns>
public static explicit operator Uri(JsonValue value)
{
return CastValue<Uri>(value);
}
/// <summary>
/// Enables explicit casts from an instance of type <see cref="System.Json.JsonValue"/> to a <see cref="System.Guid"/> object.
/// </summary>
/// <param name="value">The instance of <see cref="System.Json.JsonValue"/> used to initialize the <see cref="System.Guid"/> object.</param>
/// <returns>The <see cref="System.Guid"/> initialized with the <see cref="System.Json.JsonValue"/> value specified.</returns>
public static explicit operator Guid(JsonValue value)
{
return CastValue<Guid>(value);
}
/// <summary>
/// Enables explicit casts from an instance of type <see cref="System.Json.JsonValue"/> to a <see cref="System.DateTime"/> object.
/// </summary>
/// <param name="value">The instance of <see cref="System.Json.JsonValue"/> used to initialize the <see cref="System.DateTime"/> object.</param>
/// <returns>The <see cref="System.DateTime"/> initialized with the <see cref="System.Json.JsonValue"/> value specified.</returns>
public static explicit operator DateTime(JsonValue value)
{
return CastValue<DateTime>(value);
}
/// <summary>
/// Enables explicit casts from an instance of type <see cref="System.Json.JsonValue"/> to a <see cref="System.Char"/> object.
/// </summary>
/// <param name="value">The instance of <see cref="System.Json.JsonValue"/> used to initialize the <see cref="System.Char"/> object.</param>
/// <returns>The <see cref="System.Char"/> initialized with the <see cref="System.Json.JsonValue"/> value specified.</returns>
public static explicit operator char(JsonValue value)
{
return CastValue<char>(value);
}
/// <summary>
/// Enables explicit casts from an instance of type <see cref="System.Json.JsonValue"/> to a <see cref="System.Boolean"/> object.
/// </summary>
/// <param name="value">The instance of <see cref="System.Json.JsonValue"/> used to initialize the <see cref="System.Boolean"/> object.</param>
/// <returns>The <see cref="System.Boolean"/> initialized with the <see cref="System.Json.JsonValue"/> value specified.</returns>
public static explicit operator bool(JsonValue value)
{
return CastValue<bool>(value);
}
/// <summary>
/// Enables explicit casts from an instance of type <see cref="System.Json.JsonValue"/> to a <see cref="System.DateTimeOffset"/> object.
/// </summary>
/// <param name="value">The instance of <see cref="System.Json.JsonValue"/> used to initialize the <see cref="System.DateTimeOffset"/> object.</param>
/// <returns>The <see cref="System.DateTimeOffset"/> initialized with the <see cref="System.Json.JsonValue"/> value specified.</returns>
public static explicit operator DateTimeOffset(JsonValue value)
{
return CastValue<DateTimeOffset>(value);
}
/// <summary>
/// Enables implicit casts from type <see cref="System.Boolean"/> to a <see cref="System.Json.JsonPrimitive"/>.
/// </summary>
/// <param name="value">The <see cref="System.Boolean"/> instance used to initialize the <see cref="System.Json.JsonPrimitive"/>.</param>
/// <returns>The <see cref="System.Json.JsonValue"/> initialized with the <see cref="System.Boolean"/> specified.</returns>
public static implicit operator JsonValue(bool value)
{
return new JsonPrimitive(value);
}
/// <summary>
/// Enables implicit casts from type <see cref="System.Byte"/> to a <see cref="System.Json.JsonPrimitive"/>.
/// </summary>
/// <param name="value">The <see cref="System.Byte"/> instance used to initialize the <see cref="System.Json.JsonPrimitive"/>.</param>
/// <returns>The <see cref="System.Json.JsonValue"/> initialized with the <see cref="System.Byte"/> specified.</returns>
public static implicit operator JsonValue(byte value)
{
return new JsonPrimitive(value);
}
/// <summary>
/// Enables implicit casts from type <see cref="System.Decimal"/> to a <see cref="System.Json.JsonPrimitive"/>.
/// </summary>
/// <param name="value">The <see cref="System.Decimal"/> instance used to initialize the <see cref="System.Json.JsonPrimitive"/>.</param>
/// <returns>The <see cref="System.Json.JsonValue"/> initialized with the <see cref="System.Decimal"/> specified.</returns>
public static implicit operator JsonValue(decimal value)
{
return new JsonPrimitive(value);
}
/// <summary>
/// Enables implicit casts from type <see cref="System.Double"/> to a <see cref="System.Json.JsonPrimitive"/>.
/// </summary>
/// <param name="value">The <see cref="System.Double"/> instance used to initialize the <see cref="System.Json.JsonPrimitive"/>.</param>
/// <returns>The <see cref="System.Json.JsonValue"/> initialized with the <see cref="System.Double"/> specified.</returns>
public static implicit operator JsonValue(double value)
{
return new JsonPrimitive(value);
}
/// <summary>
/// Enables implicit casts from type <see cref="System.Int16"/> to a <see cref="System.Json.JsonPrimitive"/>.
/// </summary>
/// <param name="value">The <see cref="System.Int16"/> instance used to initialize the <see cref="System.Json.JsonPrimitive"/>.</param>
/// <returns>The <see cref="System.Json.JsonValue"/> initialized with the <see cref="System.Int16"/> specified.</returns>
public static implicit operator JsonValue(short value)
{
return new JsonPrimitive(value);
}
/// <summary>
/// Enables implicit casts from type <see cref="System.Int32"/> to a <see cref="System.Json.JsonPrimitive"/>.
/// </summary>
/// <param name="value">The <see cref="System.Int32"/> instance used to initialize the <see cref="System.Json.JsonPrimitive"/>.</param>
/// <returns>The <see cref="System.Json.JsonValue"/> initialized with the <see cref="System.Int32"/> specified.</returns>
public static implicit operator JsonValue(int value)
{
return new JsonPrimitive(value);
}
/// <summary>
/// Enables implicit casts from type <see cref="System.Int64"/> to a <see cref="System.Json.JsonPrimitive"/>.
/// </summary>
/// <param name="value">The <see cref="System.Int64"/> instance used to initialize the <see cref="System.Json.JsonPrimitive"/>.</param>
/// <returns>The <see cref="System.Json.JsonValue"/> initialized with the <see cref="System.Int64"/> specified.</returns>
public static implicit operator JsonValue(long value)
{
return new JsonPrimitive(value);
}
/// <summary>
/// Enables implicit casts from type <see cref="System.Single"/> to a <see cref="System.Json.JsonPrimitive"/>.
/// </summary>
/// <param name="value">The <see cref="System.Single"/> instance used to initialize the <see cref="System.Json.JsonPrimitive"/>.</param>
/// <returns>The <see cref="System.Json.JsonValue"/> initialized with the <see cref="System.Single"/> specified.</returns>
public static implicit operator JsonValue(float value)
{
return new JsonPrimitive(value);
}
/// <summary>
/// Enables implicit casts from type <see cref="System.String"/> to a <see cref="System.Json.JsonPrimitive"/>.
/// </summary>
/// <param name="value">The <see cref="System.String"/> instance used to initialize the <see cref="System.Json.JsonPrimitive"/>.</param>
/// <returns>The <see cref="System.Json.JsonValue"/> initialized with the <see cref="System.String"/> specified, or null if the value is null.</returns>
[SuppressMessage("Microsoft.Design", "CA1057:StringUriOverloadsCallSystemUriOverloads",
Justification = "This operator does not intend to represent a Uri overload.")]
public static implicit operator JsonValue(string value)
{
return value == null ? null : new JsonPrimitive(value);
}
/// <summary>
/// Enables implicit casts from type <see cref="System.Char"/> to a <see cref="System.Json.JsonPrimitive"/>.
/// </summary>
/// <param name="value">The <see cref="System.Char"/> instance used to initialize the <see cref="System.Json.JsonPrimitive"/>.</param>
/// <returns>The <see cref="System.Json.JsonValue"/> initialized with the <see cref="System.Char"/> specified.</returns>
public static implicit operator JsonValue(char value)
{
return new JsonPrimitive(value);
}
/// <summary>
/// Enables implicit casts from type <see cref="System.DateTime"/> to a <see cref="System.Json.JsonPrimitive"/>.
/// </summary>
/// <param name="value">The <see cref="System.DateTime"/> instance used to initialize the <see cref="System.Json.JsonPrimitive"/>.</param>
/// <returns>The <see cref="System.Json.JsonValue"/> initialized with the <see cref="System.DateTime"/> specified.</returns>
public static implicit operator JsonValue(DateTime value)
{
return new JsonPrimitive(value);
}
/// <summary>
/// Enables implicit casts from type <see cref="System.Guid"/> to a <see cref="System.Json.JsonPrimitive"/>.
/// </summary>
/// <param name="value">The <see cref="System.Guid"/> instance used to initialize the <see cref="System.Json.JsonPrimitive"/>.</param>
/// <returns>The <see cref="System.Json.JsonValue"/> initialized with the <see cref="System.Guid"/> specified.</returns>
public static implicit operator JsonValue(Guid value)
{
return new JsonPrimitive(value);
}
/// <summary>
/// Enables implicit casts from type <see cref="System.Uri"/> to a <see cref="System.Json.JsonPrimitive"/>.
/// </summary>
/// <param name="value">The <see cref="System.Uri"/> instance used to initialize the <see cref="System.Json.JsonPrimitive"/>.</param>
/// <returns>The <see cref="System.Json.JsonValue"/> initialized with the <see cref="System.Uri"/> specified, or null if the value is null.</returns>
public static implicit operator JsonValue(Uri value)
{
return value == null ? null : new JsonPrimitive(value);
}
/// <summary>
/// Enables implicit casts from type <see cref="System.SByte"/> to a <see cref="System.Json.JsonPrimitive"/>.
/// </summary>
/// <param name="value">The <see cref="System.SByte"/> instance used to initialize the <see cref="System.Json.JsonPrimitive"/>.</param>
/// <returns>The <see cref="System.Json.JsonValue"/> initialized with the <see cref="System.SByte"/> specified.</returns>
[CLSCompliant(false)]
public static implicit operator JsonValue(sbyte value)
{
return new JsonPrimitive(value);
}
/// <summary>
/// Enables implicit casts from type <see cref="System.UInt16"/> to a <see cref="System.Json.JsonPrimitive"/>.
/// </summary>
/// <param name="value">The <see cref="System.UInt16"/> instance used to initialize the <see cref="System.Json.JsonPrimitive"/>.</param>
/// <returns>The <see cref="System.Json.JsonValue"/> initialized with the <see cref="System.UInt16"/> specified.</returns>
[CLSCompliant(false)]
public static implicit operator JsonValue(ushort value)
{
return new JsonPrimitive(value);
}
/// <summary>
/// Enables implicit casts from type <see cref="System.UInt32"/> to a <see cref="System.Json.JsonPrimitive"/>.
/// </summary>
/// <param name="value">The <see cref="System.UInt32"/> instance used to initialize the <see cref="System.Json.JsonPrimitive"/>.</param>
/// <returns>The <see cref="System.Json.JsonValue"/> initialized with the <see cref="System.UInt32"/> specified.</returns>
[CLSCompliant(false)]
public static implicit operator JsonValue(uint value)
{
return new JsonPrimitive(value);
}
/// <summary>
/// Enables implicit casts from type <see cref="System.UInt64"/> to a <see cref="System.Json.JsonPrimitive"/>.
/// </summary>
/// <param name="value">The <see cref="System.UInt64"/> instance used to initialize the <see cref="System.Json.JsonPrimitive"/>.</param>
/// <returns>The <see cref="System.Json.JsonValue"/> initialized with the <see cref="System.UInt64"/> specified.</returns>
[CLSCompliant(false)]
public static implicit operator JsonValue(ulong value)
{
return new JsonPrimitive(value);
}
/// <summary>
/// Enables implicit casts from type <see cref="System.DateTimeOffset"/> to a <see cref="System.Json.JsonPrimitive"/>.
/// </summary>
/// <param name="value">The <see cref="System.DateTimeOffset"/> instance used to initialize the <see cref="System.Json.JsonPrimitive"/>.</param>
/// <returns>The <see cref="System.Json.JsonValue"/> initialized with the <see cref="System.DateTimeOffset"/> specified.</returns>
public static implicit operator JsonValue(DateTimeOffset value)
{
return new JsonPrimitive(value);
}
}
}
|