Procházet zdrojové kódy

Added more config options.

woollybah před 6 roky
rodič
revize
da77f31cb3
2 změnil soubory, kde provedl 114 přidání a 7 odebrání
  1. 62 7
      jconv.mod/jconv.bmx
  2. 52 0
      jconv.mod/tests/test.bmx

+ 62 - 7
jconv.mod/jconv.bmx

@@ -60,7 +60,7 @@ Type TJConvBuilder
 	End Rem
 	End Rem
 	Method Build:TJConv()
 	Method Build:TJConv()
 		Local jconv:TJConv = New TJConv
 		Local jconv:TJConv = New TJConv
-		jconv.options = options
+		jconv.SetOptions(options)
 		
 		
 		For Local serializer:TJConvSerializer = EachIn options.serializers.Values()
 		For Local serializer:TJConvSerializer = EachIn options.serializers.Values()
 			serializer.jconv = jconv
 			serializer.jconv = jconv
@@ -104,6 +104,31 @@ Type TJConvBuilder
 		Return Self
 		Return Self
 	End Method
 	End Method
 
 
+	Rem
+	bbdoc: Serialization of real numbers will have a maximum precision of @precision fractional digits.
+	End Rem
+	Method WithPrecision:TJConvBuilder(precision:Int)
+		options.precision = precision
+		Return Self
+	End Method
+
+	Rem
+	bbdoc: Enables pretty-printing of the serialized json, using @indent spaces of indentation.
+	about: The default, 0, disables pretty-printing. The maximum indent size is 31.
+	End Rem
+	Method WithIndent:TJConvBuilder(indent:Int)
+		options.indent = indent
+		Return Self
+	End Method
+
+	Rem
+	bbdoc: Enables compact representation, removing extra spacing.
+	End Rem
+	Method WithCompact:TJConvBuilder()
+		options.compact = True
+		Return Self
+	End Method
+	
 End Type
 End Type
 
 
 Rem
 Rem
@@ -112,6 +137,8 @@ End Rem
 Type TJConv
 Type TJConv
 
 
 	Field options:TJConvOptions = New TJConvOptions
 	Field options:TJConvOptions = New TJConvOptions
+	Field flags:Int
+	Field precision:Int = 17
 	
 	
 	Field defaultSerializer:TJConvSerializer = New TJConvSerializer
 	Field defaultSerializer:TJConvSerializer = New TJConvSerializer
 	
 	
@@ -208,7 +235,7 @@ Type TJConv
 		
 		
 			ToJson(json, obj)
 			ToJson(json, obj)
 			
 			
-			Return json.SaveString()
+			Return json.SaveString(flags, 0, precision)
 		End If
 		End If
 		
 		
 		If typeId.ExtendsType(ObjectTypeId) Then
 		If typeId.ExtendsType(ObjectTypeId) Then
@@ -220,7 +247,7 @@ Type TJConv
 
 
 			Local json:TJSON = serializer.Serialize(obj, typeId.Name())
 			Local json:TJSON = serializer.Serialize(obj, typeId.Name())
 
 
-			Return json.SaveString()
+			Return json.SaveString(flags, 0, precision)
 		End If
 		End If
 	End Method
 	End Method
 
 
@@ -266,12 +293,12 @@ Type TJConv
 		If Not obj Then
 		If Not obj Then
 			If IsEmptyArray(obj) Then
 			If IsEmptyArray(obj) Then
 				Local json:TJSONArray = New TJSONArray.Create()
 				Local json:TJSONArray = New TJSONArray.Create()
-				json.SaveStream(stream)
+				json.SaveStream(stream, flags, 0, precision)
 				Return
 				Return
 			End If
 			End If
 			
 			
 			Local json:TJSONObject = New TJSONObject.Create()
 			Local json:TJSONObject = New TJSONObject.Create()
-			json.SaveStream(stream)
+			json.SaveStream(stream, flags, 0, precision)
 			Return
 			Return
 		End If
 		End If
 
 
@@ -282,7 +309,7 @@ Type TJConv
 		
 		
 			ToJson(json, obj)
 			ToJson(json, obj)
 			
 			
-			json.SaveStream(stream)
+			json.SaveStream(stream, flags, 0, precision)
 			Return
 			Return
 		Else If typeId.ExtendsType(ObjectTypeId) Then
 		Else If typeId.ExtendsType(ObjectTypeId) Then
 
 
@@ -290,7 +317,7 @@ Type TJConv
 
 
 			ToJson(json, obj)
 			ToJson(json, obj)
 		
 		
-			json.SaveStream(stream)
+			json.SaveStream(stream, flags, 0, precision)
 		End If
 		End If
 
 
 	End Method
 	End Method
@@ -462,6 +489,27 @@ Type TJConv
 			Return f.Name()
 			Return f.Name()
 		End If
 		End If
 	End Method
 	End Method
+
+Private
+	Method SetOptions(options:TJConvOptions)
+		Self.options = options
+		
+		flags = 0
+		
+		If options.indent Then
+			flags :| (options.indent & $1F)
+		End If
+		
+		If options.compact Then
+			flags :| JSON_COMPACT
+		End If
+		
+		If options.precision >= 0 Then
+			flags :| JSON_FRACTIONAL_DIGITS
+			precision = options.precision
+		End If
+	End Method
+Public
 End Type
 End Type
 
 
 Type TJConvOptions
 Type TJConvOptions
@@ -470,6 +518,12 @@ Type TJConvOptions
 
 
 	Field serializers:TMap = New TMap
 	Field serializers:TMap = New TMap
 
 
+	Field precision:Int = -1
+	
+	Field indent:Int
+	
+	Field compact:Int
+	
 End Type
 End Type
 
 
 Rem
 Rem
@@ -858,6 +912,7 @@ End Type
 Type TBoolSerializer Extends TJConvSerializer
 Type TBoolSerializer Extends TJConvSerializer
 
 
 	Method Serialize:TJSON(source:Object, sourceType:String)
 	Method Serialize:TJSON(source:Object, sourceType:String)
+
 		Local value:TBool = TBool(source)
 		Local value:TBool = TBool(source)
 		If value Then
 		If value Then
 			Return New TJSONBool.Create(value.value)
 			Return New TJSONBool.Create(value.value)

+ 52 - 0
jconv.mod/tests/test.bmx

@@ -40,6 +40,9 @@ Type TArrayTest Extends TJConvTest
 	Const JSON_BOX_SIZE_T:String = "{~qb8~q: 100}"
 	Const JSON_BOX_SIZE_T:String = "{~qb8~q: 100}"
 	Const JSON_BOX_FLOAT:String = "{~qb9~q: 7.5}"
 	Const JSON_BOX_FLOAT:String = "{~qb9~q: 7.5}"
 	Const JSON_BOX_DOUBLE:String = "{~qb10~q: 68.418}"
 	Const JSON_BOX_DOUBLE:String = "{~qb10~q: 68.418}"
+	Const JSON_PREC:String = "{~qp1~q: 5.352, ~qp2~q: 65.698}"
+	Const JSON_SER_NAME_COMPACT:String = "{~qname~q:~qone~q,~qname1~q:~qtwo~q,~qc~q:~qthree~q}"
+	Const JSON_SER_NAME_PRETTY:String = "{~n  ~qname~q: ~qone~q,~n  ~qname1~q: ~qtwo~q,~n  ~qc~q: ~qthree~q~n}"
 
 
 	Method testEmptyObject() { test }
 	Method testEmptyObject() { test }
 		Local obj:Object
 		Local obj:Object
@@ -306,6 +309,42 @@ Type TArrayTest Extends TJConvTest
 		
 		
 	End Method
 	End Method
 
 
+	Method testPrecision() { test }
+
+		Local prec:TPrec = New TPrec(5.3521, 65.6982902)
+
+		jconv = New TJConvBuilder.WithPrecision(3).Build()
+		
+		assertEquals(JSON_PREC, jconv.ToJson(prec))
+
+	End Method
+
+	Method testCompact() { test }
+
+		jconv = New TJConvBuilder.WithCompact().Build()
+
+		Local name1:TSName = New TSName
+		name1.a = "one"
+		name1.b = "two"
+		name1.c = "three"
+		
+		assertEquals(JSON_SER_NAME_COMPACT, jconv.ToJson(name1))
+		
+	End Method
+
+	Method testIndent() { test }
+
+		jconv = New TJConvBuilder.WithIndent(2).Build()
+
+		Local name1:TSName = New TSName
+		name1.a = "one"
+		name1.b = "two"
+		name1.c = "three"
+		
+		assertEquals(JSON_SER_NAME_PRETTY, jconv.ToJson(name1))
+		
+	End Method
+	
 End Type
 End Type
 
 
 Type TData
 Type TData
@@ -452,3 +491,16 @@ Type TBoxed
 	Field b10:TDouble
 	Field b10:TDouble
 
 
 End Type
 End Type
+
+
+Type TPrec
+
+	Field p1:Float
+	Field p2:Double
+
+	Method New(p1:Float, p2:Double)
+		Self.p1 = p1
+		Self.p2 = p2
+	End Method
+	
+End Type