<commit_before>// Copyright 2014 SteelSeries ApS.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// This package implements a basic LISP interpretor for embedding in a go program for scripting.
// This file implements Json<->Lisp conversions.

package golisp

import (
	"encoding/json"
	"errors"
	"fmt"
)

func JsonToLisp(json interface{}) (result *Data) {
	mapValue, ok := json.(map[string]interface{})
	if ok {
		var alist *Data
		for key, val := range mapValue {
			value := JsonToLisp(val)
			alist = Acons(StringWithValue(key), value, alist)
		}
		return alist
	}

	arrayValue, ok := json.([]interface{})
	if ok {
		var ary *Data
		for _, val := range arrayValue {
			value := JsonToLisp(val)
			ary = Cons(value, ary)
		}
		return Reverse(ary)
	}

	intValue, ok := json.(float64)
	if ok {
		return IntegerWithValue(int64(intValue))
	}

	strValue, ok := json.(string)
	if ok {
		return StringWithValue(strValue)
	}

	boolValue, ok := json.(bool)
	if ok {
		return BooleanWithValue(boolValue)
	}

	return
}

func JsonStringToLisp(jsonData string) (result *Data) {
	b := []byte(jsonData)
	var data interface{}
	err := json.Unmarshal(b, &data)
	if err != nil {
		panic(errors.New(fmt.Sprintf("Badly formed json: '%s'", jsonData)))
	}
	return JsonToLisp(data)
}

func LispToJson(d *Data) (result interface{}) {
	if d == nil {
		return ""
	}<commit_msg>return float values in JsonToLisp if numbers aren't integers<commit_after>// Copyright 2014 SteelSeries ApS.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// This package implements a basic LISP interpretor for embedding in a go program for scripting.
// This file implements Json<->Lisp conversions.

package golisp

import (
	"encoding/json"
	"errors"
	"fmt"
	"math"
)

func JsonToLisp(json interface{}) (result *Data) {
	mapValue, ok := json.(map[string]interface{})
	if ok {
		var alist *Data
		for key, val := range mapValue {
			value := JsonToLisp(val)
			alist = Acons(StringWithValue(key), value, alist)
		}
		return alist
	}

	arrayValue, ok := json.([]interface{})
	if ok {
		var ary *Data
		for _, val := range arrayValue {
			value := JsonToLisp(val)
			ary = Cons(value, ary)
		}
		return Reverse(ary)
	}

	numValue, ok := json.(float64)
	if ok {
		if math.Trunc(numValue) == numValue {
			return IntegerWithValue(int64(numValue))
		} else {
			return FloatWithValue(float32(numValue))
		}
	}

	strValue, ok := json.(string)
	if ok {
		return StringWithValue(strValue)
	}

	boolValue, ok := json.(bool)
	if ok {
		return BooleanWithValue(boolValue)
	}

	return
}

func JsonStringToLisp(jsonData string) (result *Data) {
	b := []byte(jsonData)
	var data interface{}
	err := json.Unmarshal(b, &data)
	if err != nil {
		panic(errors.New(fmt.Sprintf("Badly formed json: '%s'", jsonData)))
	}
	return JsonToLisp(data)
}

func LispToJson(d *Data) (result interface{}) {
	if d == nil {
		return ""
	}
{'fraction_non_alphanumeric': 0.12843429328037073, 'fraction_numerical': 0.005958291956305859, 'mean_word_length': 3.774091627172196, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1016500', 'n_tokens_mistral': 1123, 'n_tokens_neox': 1042, 'n_words': 319}