aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/OSSL_Api.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--OpenSim/Region/ScriptEngine/Shared/Api/Implementation/OSSL_Api.cs187
1 files changed, 187 insertions, 0 deletions
diff --git a/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/OSSL_Api.cs b/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/OSSL_Api.cs
index 5cc58e7..7404640 100644
--- a/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/OSSL_Api.cs
+++ b/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/OSSL_Api.cs
@@ -25,6 +25,7 @@
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */ 26 */
27using System; 27using System;
28using System.Collections;
28using System.Collections.Generic; 29using System.Collections.Generic;
29using System.Runtime.Remoting.Lifetime; 30using System.Runtime.Remoting.Lifetime;
30using OpenMetaverse; 31using OpenMetaverse;
@@ -730,5 +731,191 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
730 731
731 World.ParcelMediaSetTime((float)time); 732 World.ParcelMediaSetTime((float)time);
732 } 733 }
734
735
736
737 public Hashtable osParseJSON(string JSON)
738 {
739 if (!m_OSFunctionsEnabled)
740 {
741 OSSLError("osParseJSON: permission denied");
742 return null;
743 }
744
745 CheckThreatLevel(ThreatLevel.None, "osParseJSON");
746
747 m_host.AddScriptLPS(1);
748
749 // see http://www.json.org/ for more details on JSON
750
751 string currentKey=null;
752 Stack objectStack = new Stack(); // objects in JSON can be nested so we need to keep a track of this
753 Hashtable jsondata = new Hashtable(); // the hashtable to be returned
754
755 try
756 {
757
758 // iterate through the serialised stream of tokens and store at the right depth in the hashtable
759 // the top level hashtable may contain more nested hashtables within it each containing an objects representation
760 for (int i=0;i<JSON.Length; i++)
761 {
762
763 // Console.WriteLine(""+JSON[i]);
764 switch(JSON[i])
765 {
766 case '{':
767 // create hashtable and add it to the stack or array if we are populating one, we can have a lot of nested objects in JSON
768
769 Hashtable currentObject = new Hashtable();
770 if(objectStack.Count==0) // the stack should only be empty for the first outer object
771 {
772
773 objectStack.Push(jsondata);
774 }
775 else if(objectStack.Peek().ToString()=="System.Collections.ArrayList")
776 {
777 // add it to the parent array
778 ((ArrayList)objectStack.Peek()).Add(currentObject);
779 objectStack.Push(currentObject);
780 }
781 else
782 {
783 // add it to the parent hashtable
784 ((Hashtable)objectStack.Peek()).Add(currentKey,currentObject);
785 objectStack.Push(currentObject);
786 }
787
788 // clear the key
789 currentKey=null;
790 break;
791 case '}':
792 // pop the hashtable off the stack
793 objectStack.Pop();
794 break;
795 case '"':// string boundary
796
797 string tokenValue="";
798 i++; // move to next char
799
800 // just loop through until the next quote mark storing the string
801 while(JSON[i]!='"')
802 {
803 tokenValue+=JSON[i++];
804 }
805
806 // ok we've got a string, if we've got an array on the top of the stack then we store it
807 if(objectStack.Peek().ToString()=="System.Collections.ArrayList")
808 {
809 ((ArrayList)objectStack.Peek()).Add(tokenValue);
810 }
811 else if(currentKey==null) // no key stored and its not an array this must be a key so store it
812 {
813 currentKey = tokenValue;
814 }
815 else
816 {
817 // we have a key so lets store this value
818 ((Hashtable)objectStack.Peek()).Add(currentKey,tokenValue);
819 // now lets clear the key, we're done with it and moving on
820 currentKey=null;
821 }
822
823 break;
824 case ':':// key : value separator
825 // just ignore
826 break;
827 case ' ':// spaces
828 // just ignore
829 break;
830 case '[': // array start
831 ArrayList currentArray = new ArrayList();
832
833 if(objectStack.Peek().ToString()=="System.Collections.ArrayList")
834 {
835 ((ArrayList)objectStack.Peek()).Add(currentArray);
836 }
837 else
838 {
839 ((Hashtable)objectStack.Peek()).Add(currentKey,currentArray);
840 // clear the key
841 currentKey=null;
842 }
843 objectStack.Push(currentArray);
844
845 break;
846 case ',':// seperator
847 // just ignore
848 break;
849 case ']'://Array end
850 // pop the array off the stack
851 objectStack.Pop();
852 break;
853 case 't': // we've found a character start not in quotes, it must be a boolean true
854
855 if(objectStack.Peek().ToString()=="System.Collections.ArrayList")
856 {
857 ((ArrayList)objectStack.Peek()).Add(true);
858 }
859 else
860 {
861 ((Hashtable)objectStack.Peek()).Add(currentKey,true);
862 }
863
864 //advance the counter to the letter 'e'
865 i = i+3;
866 break;
867 case 'f': // we've found a character start not in quotes, it must be a boolean false
868
869 if(objectStack.Peek().ToString()=="System.Collections.ArrayList")
870 {
871 ((ArrayList)objectStack.Peek()).Add(false);
872 }
873 else
874 {
875 ((Hashtable)objectStack.Peek()).Add(currentKey,false);
876 }
877 //advance the counter to the letter 'e'
878 i = i+4;
879 break;
880
881 default:
882 // ok here we're catching all numeric types int,double,long we might want to spit these up mr accurately
883 // but for now we'll just do them as strings
884
885 string numberValue="";
886
887 // just loop through until the next known marker quote mark storing the string
888 while(JSON[i] != '"' && JSON[i] != ',' && JSON[i] != ']' && JSON[i] != '}' && JSON[i] != ' ')
889 {
890 numberValue+=""+JSON[i++];
891 }
892
893 i--; // we want to process this caracter that marked the end of this string in the main loop
894
895 // ok we've got a string, if we've got an array on the top of the stack then we store it
896 if(objectStack.Peek().ToString()=="System.Collections.ArrayList")
897 {
898 ((ArrayList)objectStack.Peek()).Add(numberValue);
899 }
900 else
901 {
902 // we have a key so lets store this value
903 ((Hashtable)objectStack.Peek()).Add(currentKey,numberValue);
904 // now lets clear the key, we're done with it and moving on
905 currentKey=null;
906 }
907
908 break;
909 }
910 }
911 }
912 catch(Exception)
913 {
914 OSSLError("osParseJSON: The JSON string is not valid " + JSON);
915 }
916
917 return jsondata;
918 }
919
733 } 920 }
734} 921}