<commit_before>package jsonschema

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

type Example struct {
	value interface{}
}

func NewDefaultExample(t *Type) (*Example, error) {
	if t.Is(TypeNull) {
		return &Example{nil}, nil
	} else if t.Is(TypeBoolean) {
		return &Example{false}, nil
	} else if t.Is(TypeNumber) || t.Is(TypeInteger) {
		return &Example{0}, nil
	} else if t.Is(TypeString) {
		return &Example{""}, nil
	}
	return nil, fmt.Errorf("no default example: %s", t.String())
}

func (e *Example) UnmarshalJSON(data []byte) error {
	if err := json.Unmarshal(data, &e.value); err != nil {
		return err
	}
	return nil
}

func (e Example) MarshalJSON() ([]byte, error) {
	return []byte(e.String()), nil
}

func (e *Example) Initialize(def interface{}) {
	if !e.IsDefined() {
		e.value = def
	}
}

func (e *Example) IsDefined() bool {
	return e.value != nil
}

func (e Example) String() string {
	if r, ok := e.value.(string); ok {
		return fmt.Sprintf("\"%s\"", r)
	}
	return e.RawString()
}

func (e Example) RawString() string {
	switch v := e.value.(type) {
	default:
		return ""
	case []interface{}, map[string]interface{}:
		b, err := json.Marshal(v)
		if err != nil {
			return ""
		}
		return string(b)
	case string:
		return v
	case float64:
		return strconv.FormatFloat(v, 'f', -1, 64)
	case bool:
		return strconv.FormatBool(v)
	case nil:
		return "null"
	}
}<commit_msg>Support default example for number<commit_after>package jsonschema

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

type Example struct {
	value interface{}
}

func NewDefaultExample(t *Type) (*Example, error) {
	if t.Is(TypeNull) {
		return &Example{nil}, nil
	} else if t.Is(TypeBoolean) {
		return &Example{false}, nil
	} else if t.Is(TypeNumber) || t.Is(TypeInteger) {
		return &Example{0.0}, nil
	} else if t.Is(TypeString) {
		return &Example{""}, nil
	}
	return nil, fmt.Errorf("no default example: %s", t.String())
}

func (e *Example) UnmarshalJSON(data []byte) error {
	if err := json.Unmarshal(data, &e.value); err != nil {
		return err
	}
	return nil
}

func (e *Example) MarshalJSON() ([]byte, error) {
	return []byte(e.String()), nil
}

func (e *Example) Initialize(def interface{}) {
	if !e.IsDefined() {
		e.value = def
	}
}

func (e *Example) IsDefined() bool {
	return e.value != nil
}

func (e Example) String() string {
	if r, ok := e.value.(string); ok {
		return fmt.Sprintf("\"%s\"", r)
	}
	return e.RawString()
}

func (e Example) RawString() string {
	switch v := e.value.(type) {
	default:
		return ""
	case []interface{}, map[string]interface{}:
		b, err := json.Marshal(v)
		if err != nil {
			return ""
		}
		return string(b)
	case string:
		return v
	// case int:
	// 	i := int64(v)
	// 	log.Println("->", i)
	// 	return strconv.FormatInt(i, 10)
	case float64:
		return strconv.FormatFloat(v, 'f', -1, 64)
	case bool:
		return strconv.FormatBool(v)
	case nil:
		return "null"
	}
}
{'fraction_non_alphanumeric': 0.17287784679089027, 'fraction_numerical': 0.005866114561766736, 'mean_word_length': 3.706168831168831, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '4372322', 'n_tokens_mistral': 1153, 'n_tokens_neox': 1102, 'n_words': 303}