You've already forked json-iterator
							
							
				mirror of
				https://github.com/json-iterator/go.git
				synced 2025-10-31 00:07:40 +02:00 
			
		
		
		
	update extension
This commit is contained in:
		| @@ -45,8 +45,8 @@ func Test_customize_field_decoder(t *testing.T) { | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func Test_customize_field_decoder_factory(t *testing.T) { | ||||
| 	RegisterFieldCustomizer(func(type_ reflect.Type, field *reflect.StructField) ([]string, DecoderFunc) { | ||||
| func Test_customize_field_by_extension(t *testing.T) { | ||||
| 	RegisterExtension(func(type_ reflect.Type, field *reflect.StructField) ([]string, DecoderFunc) { | ||||
| 		if (type_.String() == "jsoniter.Tom" && field.Name == "field1") { | ||||
| 			return []string{"field-1"}, func(ptr unsafe.Pointer, iter *Iterator) { | ||||
| 				*((*string)(ptr)) = strconv.Itoa(iter.ReadInt()) | ||||
| @@ -62,4 +62,28 @@ func Test_customize_field_decoder_factory(t *testing.T) { | ||||
| 	if tom.field1 != "100" { | ||||
| 		t.Fatal(tom.field1) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| type Jerry struct { | ||||
| 	field1 string | ||||
| } | ||||
|  | ||||
| func Test_customize_type_by_extension(t *testing.T) { | ||||
| 	RegisterExtension(func(type_ reflect.Type, field *reflect.StructField) ([]string, DecoderFunc) { | ||||
| 		if (type_.String() == "jsoniter.Jerry" && field == nil) { | ||||
| 			return nil, func(ptr unsafe.Pointer, iter *Iterator) { | ||||
| 				obj := (*Jerry)(ptr) | ||||
| 				obj.field1 = iter.ReadString() | ||||
| 			} | ||||
| 		} | ||||
| 		return nil, nil | ||||
| 	}) | ||||
| 	jerry := Jerry{} | ||||
| 	err := Unmarshal([]byte(`"100"`), &jerry) | ||||
| 	if err != nil { | ||||
| 		t.Fatal(err) | ||||
| 	} | ||||
| 	if jerry.field1 != "100" { | ||||
| 		t.Fatal(jerry.field1) | ||||
| 	} | ||||
| } | ||||
		Reference in New Issue
	
	Block a user