Sample code for reading and traversing an EDN structure. EDN is a more flexible data interchange format, than JSON.
Thanks everyone, for their help with some of the JavaObject details.
https://github.com/bpsm/edn-java
Thanks everyone, for their help with some of the JavaObject details.
https://github.com/bpsm/edn-java
B4X:
'Non-UI application (console / server application)
#Region Project Attributes
#CommandLineArgs:
#MergeLibraries: True
#AdditionalJar: edn-java-0.5.0.jar
#End Region
Sub Process_Globals
Private NativeMe As JavaObject
End Sub
Sub AppStart (Args() As String)
NativeMe = Me
Private jo_Object As JavaObject
Private s_EDN_Input As String
' s_EDN_Input = "[ 1, 2, 05/13/2017, 3, \v, 5 ]"
' jo_Object = GetParsedEDNObject( s_EDN_Input )
' s_EDN_Input = "[ 1, 2, 3, \newline, /#_discarded, 5, { :a 15, :b 16 }, ( 19, 13, 18, #{1 0 15 122 2 9 3 7 5} ) ]"
' s_EDN_Input = "[ 1, 2, 3, \newline, 5, thissymbol #uuid " & Chr(34) & "f81d4fae-7dec-11d0-a765-00a0c91e6bf6" & Chr(34) & ", { :a 15, :b 16 }, ( 19, 13, 18, #{1 0 15 122 2 9 3 7 5} ) ]"
s_EDN_Input = "[ 1, #inst " & Chr(34) & "1985-04-12T23:20:50.52Z" & Chr(34) & ", 2, 3, \newline, 5, thissymbol #uuid " & Chr(34) & "f81d4fae-7dec-11d0-a765-00a0c91e6bf6" & Chr(34) & ", { :a 15, :b 16 }, ( 19, 13, 18, #{1 0 15 122 2 9 3 7 5} ) ]"
jo_Object = GetParsedEDNObject( s_EDN_Input )
' s_EDN_Input = "{:x 1, :y 2}"
' jo_Object = GetParsedEDNObject( s_EDN_Input )
' s_EDN_Input = "( + :five (* 7 9 ) )"
' jo_Object = GetParsedEDNObject( s_EDN_Input )
' s_EDN_Input = "#{1 0 2 9 3 8 4 7 5 6}"
' jo_Object = GetParsedEDNObject( s_EDN_Input )
' s_EDN_Input = "( 1, 2, 3, 4, 5, 6.334 )"
' jo_Object = GetParsedEDNObject( s_EDN_Input )
' s_EDN_Input = "[ 9, 6, 7, 3, \a ]"
' jo_Object = GetParsedEDNObject( s_EDN_Input )
' s_EDN_Input = "#{18 23 294 50}"
' jo_Object = GetParsedEDNObject( s_EDN_Input )
s_EDN_Input = ":xyz"
jo_Object = GetParsedEDNObject( s_EDN_Input )
' s_EDN_Input = 12345.74
' jo_Object = GetParsedEDNObject( s_EDN_Input )
' s_EDN_Input = 12345
' jo_Object = GetParsedEDNObject( s_EDN_Input )
' s_EDN_Input = 1
' jo_Object = GetParsedEDNObject( s_EDN_Input )
' s_EDN_Input = False
' jo_Object = GetParsedEDNObject( s_EDN_Input )
ExitApplication2( 0 )
StartMessageLoop
End Sub
Sub GetParsedEDNObject( s_EDN As String ) As JavaObject
' Via JavaObject
Private jo_EDN As JavaObject
Private jo_DefaultConfiguration As JavaObject
Private jo_Parseable As JavaObject
Private jo_Parsers As JavaObject
Private jo_Parser As JavaObject
Private jo_KeyWord As JavaObject
Private jo_NewKeyWord As JavaObject
Private jo_Value As JavaObject
Private s_ClassName As String
Log( "The edn string is " & s_EDN )
jo_Parsers.InitializeStatic( "us.bpsm.edn.parser.Parsers" )
jo_Parseable = jo_Parsers.RunMethod( "newParseable", Array( s_EDN ) )
jo_DefaultConfiguration = jo_Parsers.RunMethod( "defaultConfiguration", Null )
jo_Parser = jo_Parsers.RunMethod( "newParser", Array( jo_DefaultConfiguration ) )
jo_Value = jo_Parser.RunMethod( "nextValue", Array( jo_Parseable ) )
ParseStructure( jo_Value )
s_ClassName = NativeMe.RunMethod( "ObjectType", Array As Object( jo_Value ) )
Private jo_Map As Map
If s_ClassName == "Map" Then
jo_Map.Initialize
Private jo_EntrySet As JavaObject
Private jo_KeySet As JavaObject
Private jo_Values As JavaObject
Private n_NumberOfEntries
Private lst_KeySet As List
lst_KeySet.Initialize
jo_EntrySet = jo_Value.RunMethod( "entrySet", Null )
Private s_Key As String
jo_KeySet = jo_Value.RunMethod( "keySet", Null )
jo_Values = jo_Value.RunMethod( "values", Null )
s_Key = jo_Value.RunMethod( "get", Array( "x" ) )
n_NumberOfEntries = jo_Value.RunMethod( "size", Null )
End If
Return jo_Value
End Sub
Sub ParseStructure( jo_Structure As JavaObject )
Private s_ClassName As String
s_ClassName = NativeMe.RunMethod( "ObjectType", Array As Object( jo_Structure ) )
Private n_Size As Int
Private n_Nth_Element As Int
Private jo_Element As JavaObject
Private n_Count As Int
Private jo_Iterator As JavaObject
Select s_ClassName
Case "List"
n_Size = jo_Structure.RunMethod( "size", Null )
Log( s_ClassName & " " & jo_Structure )
For n_Nth_Element = 0 To n_Size - 1
jo_Element = jo_Structure.RunMethod( "get", Array(n_Nth_Element ) )
ParseStructure( jo_Element )
Next
Case "Vector"
n_Size = jo_Structure.RunMethod( "size", Null )
Log( s_ClassName & " " & jo_Structure )
For n_Nth_Element = 0 To n_Size - 1
jo_Element = jo_Structure.RunMethod( "get", Array(n_Nth_Element ) )
ParseStructure( jo_Element )
Next
Case "Set"
n_Size = jo_Structure.RunMethod( "size", Null )
jo_Iterator = jo_Structure.RunMethod( "iterator", Null )
Private jo_Element As JavaObject
Log( s_ClassName & " " & jo_Structure )
Do While jo_Iterator.RunMethod( "hasNext", Null )
jo_Element = jo_Iterator.RunMethod( "next", Null )
ParseStructure( jo_Element )
Loop
Case "Map"
n_Size = jo_Structure.RunMethod( "size", Null )
Private jo_EntrySet As JavaObject
Log( s_ClassName & " " & jo_Structure )
jo_EntrySet = jo_Structure.RunMethod( "entrySet", Null )
n_Size = jo_EntrySet.RunMethod( "size", Null )
jo_Iterator = jo_EntrySet.RunMethod( "iterator", Null )
Private jo_Element As JavaObject
Do While jo_Iterator.RunMethod( "hasNext", Null )
jo_Element = jo_Iterator.RunMethod( "next", Null )
Private jo_Key As JavaObject
jo_Key = jo_Element.RunMethod( "getKey", Null )
Log( "Next Key: " & jo_Key )
Private jo_Value As JavaObject
jo_Value = jo_Element.RunMethod( "getValue", Null )
Log( "Next Value: " & jo_Value )
Loop
Case "Keyword"
Log( "Keyword: " & jo_Structure )
Case "Long"
Log( "Long: " & jo_Structure )
Case "Double"
Log( "Double: " & jo_Structure )
Case "Integer"
Log( "Integer: " & jo_Structure )
Case "Short"
Log( "Short: " & jo_Structure )
Case "Float"
Log( "Float: " & jo_Structure )
Case "Boolean"
Log( "Boolean: " & jo_Structure )
Case "Symbol"
Log( "Symbol: " & jo_Structure )
Case "Token"
Log( "Token: " & jo_Structure )
Case "Character"
Log( "Character: " & jo_Structure )
Case "UUID"
Log( "UUid: " & jo_Structure )
Case "Timestamp"
Log( "Timestamp: " & jo_Structure )
End Select
End Sub
#If Java
public static String ObjectType( Object o_Object ) {
Class cls = o_Object.getClass() ;
String s_ObjectType = "" ;
s_ObjectType = cls.toString() ;
// BA.Log( "Class name is :" ) ;
// BA.Log( s_ObjectType );
if ( s_ObjectType.equals("class java.util.Collections$UnmodifiableRandomAccessList" ) ) {
s_ObjectType = "Vector" ;
} else if ( o_Object instanceof List ) {
s_ObjectType = "List" ;
} else if ( o_Object instanceof Map ) {
s_ObjectType = "Map" ;
} else if ( o_Object instanceof java.util.Set ) {
s_ObjectType = "Set" ;
} else if ( s_ObjectType.equals("class java.util.Collections$UnmodifiableMap$UnmodifiableEntrySet$UnmodifiableEntry" ) ) {
s_ObjectType = "EntrySet" ;
} else if (o_Object instanceof us.bpsm.edn.parser.Token) {
s_ObjectType = "Token" ;
} else if (o_Object instanceof us.bpsm.edn.Keyword) {
s_ObjectType = "Keyword" ;
} else if (o_Object instanceof us.bpsm.edn.TaggedValue) {
s_ObjectType = "TaggedValue" ;
} else if (o_Object instanceof us.bpsm.edn.Symbol) {
s_ObjectType = "Symbol" ;
} else if (o_Object instanceof Boolean) {
s_ObjectType = "Boolean" ;
} else if (o_Object instanceof Byte) {
s_ObjectType = "Byte" ;
} else if (o_Object instanceof CharSequence) {
s_ObjectType = "CharSequence" ;
} else if (o_Object instanceof Character) {
s_ObjectType = "Character" ;
} else if (o_Object instanceof Double) {
s_ObjectType = "Double" ;
} else if (o_Object instanceof Float) {
s_ObjectType = "Float" ;
} else if (o_Object instanceof Integer) {
s_ObjectType = "Integer" ;
} else if (o_Object instanceof Long) {
s_ObjectType = "Long" ;
} else if (o_Object instanceof Short) {
s_ObjectType = "Short" ;
} else if (o_Object instanceof java.math.BigInteger) {
s_ObjectType = "BigInteger" ;
} else if (o_Object instanceof java.sql.Timestamp) {
s_ObjectType = "BigDecimal" ;
} else if (o_Object instanceof java.util.Date) {
s_ObjectType = "Timestamp" ;
} else if (o_Object instanceof java.util.GregorianCalendar) {
s_ObjectType = "GregorianCalendar" ;
} else if (o_Object instanceof java.util.UUID) {
s_ObjectType = "UUID" ;
} else {
s_ObjectType = "Unknown" ;
return s_ObjectType ;
}
return s_ObjectType ;
}
#End If
#If Java
// A scratchpad function for testing and development
import static java.lang.System.out;
import java.util.Map;
import java.util.List ;
import us.bpsm.edn.parser.Parseable;
import us.bpsm.edn.parser.Parser ;
import us.bpsm.edn.parser.Parsers ;
import static us.bpsm.edn.parser.Parsers.defaultConfiguration ;
public static Object TestParse( String s_TestString ) {
String Result_str = "";
Class cls ;
String ClassName_str = "";
out.println( "Before assigning parser from Default Configuration" ) ;
Parser p = Parsers.newParser(defaultConfiguration()) ;
BA.Log( "p.toString():");
BA.Log( p.toString() ) ;
BA.Log( "Before getting Parseable for List object" );
Parseable pbr = Parsers.newParseable( s_TestString ) ;
BA.Log( "After getting Parseable for List object" );
cls = pbr.getClass() ;
ClassName_str = cls.getName() ;
BA.Log( "Type of object pbr:" );
BA.Log( ClassName_str );
Result_str = pbr.toString() ;
BA.Log( "Parseable List object string:" );
BA.Log( Result_str );
BA.Log( "Before assigning List object" ) ;
Object data = p.nextValue(pbr) ;
BA.Log( "After assigning List object" ) ;
if( data instanceof List ) {
BA.Log( "This was a List" ) ;
}
if( data instanceof Map ) {
BA.Log( "This was a Map" ) ;
}
BA.Log( "data.toString():" );
BA.Log( data.toString() ) ;
BA.Log( "*******************************" ) ;
return data ;
}
#End If