You've already forked json-iterator
							
							
				mirror of
				https://github.com/json-iterator/go.git
				synced 2025-10-31 00:07:40 +02:00 
			
		
		
		
	support naming strategy
This commit is contained in:
		
							
								
								
									
										39
									
								
								extra/naming_strategy.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								extra/naming_strategy.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,39 @@ | ||||
| package extra | ||||
|  | ||||
| import ( | ||||
| 	"github.com/json-iterator/go" | ||||
| 	"unicode" | ||||
| ) | ||||
|  | ||||
| func SetNamingStrategy(translate func(string) string) { | ||||
| 	jsoniter.RegisterExtension(&namingStrategyExtension{jsoniter.DummyExtension{}, translate}) | ||||
| } | ||||
|  | ||||
| type namingStrategyExtension struct { | ||||
| 	jsoniter.DummyExtension | ||||
| 	translate func(string) string | ||||
| } | ||||
|  | ||||
| func (extension *namingStrategyExtension) UpdateStructDescriptor(structDescriptor *jsoniter.StructDescriptor) { | ||||
| 	for _, binding := range structDescriptor.Fields { | ||||
| 		binding.ToNames = []string{extension.translate(binding.Field.Name)} | ||||
| 		binding.FromNames = []string{extension.translate(binding.Field.Name)} | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func LowerCaseWithUnderscores(name string) string { | ||||
| 	newName := []rune{} | ||||
| 	for i, c := range name { | ||||
| 		if i == 0 { | ||||
| 			newName = append(newName, unicode.ToLower(c)) | ||||
| 		} else { | ||||
| 			if unicode.IsUpper(c) { | ||||
| 				newName = append(newName, '_') | ||||
| 				newName = append(newName, unicode.ToLower(c)) | ||||
| 			} else { | ||||
| 				newName = append(newName, c) | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
| 	return string(newName) | ||||
| } | ||||
							
								
								
									
										23
									
								
								extra/naming_strategy_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								extra/naming_strategy_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,23 @@ | ||||
| package extra | ||||
|  | ||||
| import ( | ||||
| 	"testing" | ||||
| 	"github.com/json-iterator/go" | ||||
| 	"github.com/json-iterator/go/require" | ||||
| ) | ||||
|  | ||||
| func Test_lower_case_with_underscores(t *testing.T) { | ||||
| 	should := require.New(t) | ||||
| 	should.Equal("hello_world", LowerCaseWithUnderscores("helloWorld")) | ||||
| 	should.Equal("hello_world", LowerCaseWithUnderscores("HelloWorld")) | ||||
| 	SetNamingStrategy(LowerCaseWithUnderscores) | ||||
| 	output, err := jsoniter.MarshalToString(struct { | ||||
| 		HelloWorld string | ||||
| 	}{ | ||||
| 		HelloWorld: "hi", | ||||
| 	}) | ||||
| 	should.Nil(err) | ||||
| 	should.Equal(`{"hello_world":"hi"}`, output) | ||||
| } | ||||
|  | ||||
|  | ||||
| @@ -60,3 +60,11 @@ func Test_use_number(t *testing.T) { | ||||
| 	should.Nil(decoder2.Decode(&obj2)) | ||||
| 	should.Equal(json.Number("123"), obj2) | ||||
| } | ||||
|  | ||||
| func Test_use_number_for_unmarshal(t *testing.T) { | ||||
| 	should := require.New(t) | ||||
| 	api := Config{UseNumber: true}.Froze() | ||||
| 	var obj interface{} | ||||
| 	should.Nil(api.UnmarshalFromString("123", &obj)) | ||||
| 	should.Equal(json.Number("123"), obj) | ||||
| } | ||||
|   | ||||
| @@ -68,11 +68,11 @@ func Test_decode_int_key_map(t *testing.T) { | ||||
|  | ||||
| func Test_encode_TextMarshaler_key_map(t *testing.T) { | ||||
| 	should := require.New(t) | ||||
| 	f, _, _ := big.ParseFloat("1", 10, 64, big.ToZero) | ||||
| 	val := map[*big.Float]string{f: "2"} | ||||
| 	str, err := MarshalToString(val) | ||||
| f, _, _ := big.ParseFloat("1", 10, 64, big.ToZero) | ||||
| val := map[*big.Float]string{f: "2"} | ||||
| str, err := MarshalToString(val) | ||||
| 	should.Nil(err) | ||||
| 	should.Equal(`{"1":"2"}`, str) | ||||
| should.Equal(`{"1":"2"}`, str) | ||||
| } | ||||
|  | ||||
| func Test_decode_TextMarshaler_key_map(t *testing.T) { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user