go icon indicating copy to clipboard operation
go copied to clipboard

Jsoniter is able to unmarshall some fields and randomy stops unmarshalling

Open xceejay opened this issue 3 years ago • 1 comments

Hello, when i use the standard library to unmarshall my data i get accurate number of values which are 1000 values, but when i use jsoniter it unmarshals only 4 objects from the array

package main

import (
	"fmt"
	"io/ioutil"
	"log"

	jsoniter "github.com/json-iterator/go"
)

type Users []User

type User struct {
	Gender     string   `json:"gender"`
	Name       Name     `json:"name"`
	Location   Location `json:"location"`
	Email      string   `json:"email"`
	Login      Login    `json:"login"`
	Dob        Dob      `json:"dob"`
	Registered Dob      `json:"registered"`
	Phone      string   `json:"phone"`
	Cell       string   `json:"cell"`
	ID         ID       `json:"id"`
	Picture    Picture  `json:"picture"`
	Nat        string   `json:"nat"`
}

type Dob struct {
	Date string `json:"date"`
	Age  int64  `json:"age"`
}

type ID struct {
	Name  string `json:"name"`
	Value string `json:"value"`
}

type Location struct {
	Street      Street      `json:"street"`
	City        string      `json:"city"`
	State       string      `json:"state"`
	Country     string      `json:"country"`
	Postcode    int64       `json:"postcode"`
	Coordinates Coordinates `json:"coordinates"`
	Timezone    Timezone    `json:"timezone"`
}

type Coordinates struct {
	Latitude  string `json:"latitude"`
	Longitude string `json:"longitude"`
}

type Street struct {
	Number int64  `json:"number"`
	Name   string `json:"name"`
}

type Timezone struct {
	Offset      string `json:"offset"`
	Description string `json:"description"`
}

type Login struct {
	UUID     string `json:"uuid"`
	Username string `json:"username"`
	Password string `json:"password"`
	Salt     string `json:"salt"`
	Md5      string `json:"md5"`
	Sha1     string `json:"sha1"`
	Sha256   string `json:"sha256"`
}

type Name struct {
	Title string `json:"title"`
	First string `json:"first"`
	Last  string `json:"last"`
}

type Picture struct {
	Large     string `json:"large"`
	Medium    string `json:"medium"`
	Thumbnail string `json:"thumbnail"`
}


var json = jsoniter.ConfigCompatibleWithStandardLibrary

func main() {

	var users Users
	jsonfile := "random_users.json"
	jsondata, err := ioutil.ReadFile(jsonfile)

	if err != nil {

		checkErr(err)

	}

	json.Unmarshal(jsondata, &users)

	for key, user := range users {

		fmt.Printf("user%v: = %s \n", key, user.Name.First)
	}

}

func checkErr(err error) {
	log.Fatalf("ERROR: %v", err)
}

xceejay avatar Jun 12 '21 13:06 xceejay

type Data struct{
    MyData string
}

func main(){
    var mydata MyData
    data :=  `{"myData": "datajio\r\njiosjdof"}`
    if err := jsoniter.Unmarshal([]byte(data),&mydata); err !=nil{
       fmt.Println(err)
   }

  // only have {"myData": "datajio , the \r\n can't unmarshal 
   fmt.Printf("%v",mydata)
}

sober-wang avatar Jun 17 '21 09:06 sober-wang