MCPcopy Create free account
hub / github.com/crossoverJie/gscript / JSON

Function JSON

internal_function.go:203–257  ·  view source on GitHub ↗
(v *Visitor, ctx *parser.FunctionCallContext)

Source from the content-addressed store, hash-verified

201}
202
203func JSON(v *Visitor, ctx *parser.FunctionCallContext) interface{} {
204 paramValues := v.buildParamValues(ctx)
205 value := paramValues[0]
206 switch value.(type) {
207 case *stack.ClassObject:
208 classObject := value.(*stack.ClassObject)
209 data := v.classObject2Map(classObject)
210 marshal, err := json.Marshal(data)
211 if err != nil {
212 log.RuntimePanic(ctx, fmt.Sprintf("JSON function error occurred,error:%s", err))
213 }
214 return string(marshal)
215 case []interface{}:
216 dataList := value.([]interface{})
217 var dataClass []interface{}
218 for _, data := range dataList {
219 switch data.(type) {
220 case *LeftValue:
221 // Person[] list = {p1,p2}; string j = JSON(list);
222 leftValue := data.(*LeftValue)
223 getValue := leftValue.GetValue()
224 classObject, ok := getValue.(*stack.ClassObject)
225 if ok {
226 object2Map := v.classObject2Map(classObject)
227 dataClass = append(dataClass, object2Map)
228 }
229
230 }
231 }
232 if dataClass != nil {
233 marshal, err := json.Marshal(dataClass)
234 if err != nil {
235 log.RuntimePanic(ctx, fmt.Sprintf("JSON function error occurred,error:%s", err))
236
237 }
238 return string(marshal)
239 }
240 // int[] a = {1,2,3}; json = JSON(a)
241 marshal, err := json.Marshal(dataList)
242 if err != nil {
243 log.RuntimePanic(ctx, fmt.Sprintf("JSON function error occurred,error:%s", err))
244
245 }
246 // {1,2,3}
247 return string(marshal)
248 default:
249 marshal, err := json.Marshal(value)
250 if err != nil {
251 log.RuntimePanic(ctx, fmt.Sprintf("JSON function error occurred,error:%s", err))
252
253 }
254 return string(marshal)
255 }
256 return ""
257}
258
259func (v *Visitor) classObject2Map(classObject *stack.ClassObject) map[string]interface{} {
260 data := make(map[string]interface{})

Callers

nothing calls this directly

Calls 4

RuntimePanicFunction · 0.92
buildParamValuesMethod · 0.80
classObject2MapMethod · 0.80
GetValueMethod · 0.65

Tested by

no test coverage detected