JSON.as 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. Copyright (c) 2008, Adobe Systems Incorporated
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. * Redistributions of source code must retain the above copyright notice,
  8. this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. * Neither the name of Adobe Systems Incorporated nor the names of its
  13. contributors may be used to endorse or promote products derived from
  14. this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  16. IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  17. THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. package com.adobe.serialization.json
  28. {
  29. /**
  30. * This class provides encoding and decoding of the JSON format.
  31. *
  32. * Example usage:
  33. * <code>
  34. * // create a JSON string from an internal object
  35. * JSON.encode( myObject );
  36. *
  37. * // read a JSON string into an internal object
  38. * var myObject:Object = JSON.decode( jsonString );
  39. * </code>
  40. */
  41. public class JSON
  42. {
  43. /**
  44. * Encodes a object into a JSON string.
  45. *
  46. * @param o The object to create a JSON string for
  47. * @return the JSON string representing o
  48. * @langversion ActionScript 3.0
  49. * @playerversion Flash 9.0
  50. * @tiptext
  51. */
  52. public static function encode( o:Object ):String
  53. {
  54. return new JSONEncoder( o ).getString();
  55. }
  56. /**
  57. * Decodes a JSON string into a native object.
  58. *
  59. * @param s The JSON string representing the object
  60. * @param strict Flag indicating if the decoder should strictly adhere
  61. * to the JSON standard or not. The default of <code>true</code>
  62. * throws errors if the format does not match the JSON syntax exactly.
  63. * Pass <code>false</code> to allow for non-properly-formatted JSON
  64. * strings to be decoded with more leniancy.
  65. * @return A native object as specified by s
  66. * @throw JSONParseError
  67. * @langversion ActionScript 3.0
  68. * @playerversion Flash 9.0
  69. * @tiptext
  70. */
  71. public static function decode( s:String, strict:Boolean = true ):*
  72. {
  73. return new JSONDecoder( s, strict ).getValue();
  74. }
  75. }
  76. }