mbdavid / LiteDB

LiteDB - A .NET NoSQL Document Store in a single data file
http://www.litedb.org
MIT License
8.62k stars 1.25k forks source link

[BUG] deserialize an array json to include in database #2154

Closed FrenchFriend closed 2 years ago

FrenchFriend commented 2 years ago

Version Net 6 Describe the bug

exception when i try to deserialize a json array

Code to Reproduce

my code:

using LiteDB;
using System;
using System.Linq;

namespace NoSQL
{
    public class Program
    {
        static void Main(string[] args)
        {
            using (var db = new LiteDatabase(@"filename=C:\folder\file.db"))
            {
                var docs = JsonSerializer.DeserializeArray(@"C:\folder\agences.json").Select(x => x.AsDocument);;
                var Balance = db.GetCollection("agences").Insert(docs);
            }
        }
    }

my json file:

[
       {
        "code": "ARA0014",
        "libelle": "CLERMONT PRE LA REINE",
        "libelleEtendu": "Agence Pôle emploi CLERMONT PRE LA REINE",
        "SIRET": "13000548126460",
        "type": "APE",
        "typeAccueil": "3",
        "codeRegionINSEE": "84",
        "dispositifADEDA": true,
        "contact": {
            "email": "ale.clermontprelareine.63019@pole-emploi.fr",
            "telephonePublic": "39-49"
        },
        "adressePrincipale": {
            "bureauDistributeur": "63020",
            "communeImplantation": "63113",
            "gpsLat": 45.781788,
            "gpsLon": 3.111362,
            "ligne4": "20 RUE PRE LA REINE",
            "ligne5": "CS 90015 ",
            "ligne6": "63020 CLERMONT FERRAND CEDEX 2"
        },
        "horaires": [
            {
                "fermetureApresMidiRDV": "16:30",
                "fermetureMatin": "12:30",
                "horaireEnContinu": "N",
                "horaireEnContinuRDV": "N",
                "horaireFerme": "N",
                "horaireFermeRDV": "N",
                "jour": 1,
                "ouvertureApresMidiRDV": "12:30",
                "ouvertureMatin": "08:30"
            },
            {
                "fermetureApresMidiRDV": "16:30",
                "fermetureMatin": "12:30",
                "horaireEnContinu": "N",
                "horaireEnContinuRDV": "N",
                "horaireFerme": "N",
                "horaireFermeRDV": "N",
                "jour": 2,
                "ouvertureApresMidiRDV": "12:30",
                "ouvertureMatin": "08:30"
            },
            {
                "fermetureApresMidiRDV": "16:30",
                "fermetureMatin": "12:30",
                "horaireEnContinu": "N",
                "horaireEnContinuRDV": "N",
                "horaireFerme": "N",
                "horaireFermeRDV": "N",
                "jour": 3,
                "ouvertureApresMidiRDV": "12:30",
                "ouvertureMatin": "08:30"
            },
            {
                "fermetureMatin": "12:30",
                "horaireEnContinu": "N",
                "horaireEnContinuRDV": "N",
                "horaireFerme": "N",
                "horaireFermeRDV": "O",
                "jour": 4,
                "ouvertureMatin": "08:30"
            },
            {
                "fermetureApresMidiRDV": "15:30",
                "fermetureMatin": "12:30",
                "horaireEnContinu": "N",
                "horaireEnContinuRDV": "N",
                "horaireFerme": "N",
                "horaireFermeRDV": "N",
                "jour": 5,
                "ouvertureApresMidiRDV": "12:30",
                "ouvertureMatin": "08:30"
            }
        ]
    },
    {
        "code": "ARA0018",
        "libelle": "CLERMONT JOUHAUX",
        "libelleEtendu": "Agence Pôle emploi CLERMONT JOUHAUX",
        "SIRET": "13000548120448",
        "type": "APE",
        "typeAccueil": "3",
        "codeRegionINSEE": "84",
        "dispositifADEDA": true,
        "contact": {
            "email": "aleclermontjouhaux.63000@pole-emploi.fr",
            "telephonePublic": "39-49"
        },
        "adressePrincipale": {
            "bureauDistributeur": "63050",
            "communeImplantation": "63113",
            "gpsLat": 45.796812,
            "gpsLon": 3.116937,
            "ligne4": "68 BOULEVARD LEON JOUHAUX",
            "ligne5": "CS 60703 ",
            "ligne6": "63050 CLERMONT FERRAND CEDEX 2"
        },
        "horaires": [
            {
                "fermetureApresMidiRDV": "16:30",
                "fermetureMatin": "12:30",
                "horaireEnContinu": "N",
                "horaireEnContinuRDV": "N",
                "horaireFerme": "N",
                "horaireFermeRDV": "N",
                "jour": 1,
                "ouvertureApresMidiRDV": "12:30",
                "ouvertureMatin": "08:30"
            },
            {
                "fermetureApresMidiRDV": "16:30",
                "fermetureMatin": "12:30",
                "horaireEnContinu": "N",
                "horaireEnContinuRDV": "N",
                "horaireFerme": "N",
                "horaireFermeRDV": "N",
                "jour": 2,
                "ouvertureApresMidiRDV": "12:30",
                "ouvertureMatin": "08:30"
            },
            {
                "fermetureApresMidiRDV": "16:30",
                "fermetureMatin": "12:30",
                "horaireEnContinu": "N",
                "horaireEnContinuRDV": "N",
                "horaireFerme": "N",
                "horaireFermeRDV": "N",
                "jour": 3,
                "ouvertureApresMidiRDV": "12:30",
                "ouvertureMatin": "08:30"
            },
            {
                "fermetureMatin": "12:30",
                "horaireEnContinu": "N",
                "horaireEnContinuRDV": "N",
                "horaireFerme": "N",
                "horaireFermeRDV": "O",
                "jour": 4,
                "ouvertureMatin": "08:30"
            },
            {
                "fermetureApresMidiRDV": "15:30",
                "fermetureMatin": "12:30",
                "horaireEnContinu": "N",
                "horaireEnContinuRDV": "N",
                "horaireFerme": "N",
                "horaireFermeRDV": "N",
                "jour": 5,
                "ouvertureApresMidiRDV": "12:30",
                "ouvertureMatin": "08:30"
            }
        ]
    }
]

Expected behavior

Screenshots/Stacktrace

LiteDB.LiteException HResult=0x80131500 Message=Unexpected token C in position 1. Source=LiteDB Arborescence des appels de procédure : à LiteDB.Token.Expect(TokenType type) dans C:\Users\itki4060\Desktop\DBLite\LiteDB\Utils\Tokenizer.cs :ligne 125 à LiteDB.JsonReader.d7.MoveNext() dans C:\Users\itki4060\Desktop\DBLite\LiteDB\Document\Json\JsonReader.cs :ligne 49 à System.Linq.Enumerable.SelectEnumerableIterator2.MoveNext() à LiteDB.LiteCollection1.d52.MoveNext() dans C:\Users\itki4060\Desktop\DBLite\LiteDB\Client\Database\Collections\Insert.cs :ligne 74 à LiteDB.Engine.LiteEngine.<>c__DisplayClass7_0.b__0(TransactionService transaction) dans C:\Users\itki4060\Desktop\DBLite\LiteDB\Engine\Engine\Insert.cs :ligne 29 à LiteDB.Engine.LiteEngine.AutoTransaction[T](Func2 fn) dans C:\Users\itki4060\Desktop\DBLite\LiteDB\Engine\Engine\Transaction.cs :ligne 79 à LiteDB.Engine.LiteEngine.Insert(String collection, IEnumerable1 docs, BsonAutoId autoId) dans C:\Users\itki4060\Desktop\DBLite\LiteDB\Engine\Engine\Insert.cs :ligne 20 à LiteDB.LiteCollection1.Insert(IEnumerable1 entities) dans C:\Users\itki4060\Desktop\DBLite\LiteDB\Client\Database\Collections\Insert.cs :ligne 55 à NoSQL.Program.Main(String[] args) dans C:\Projets\Partoo\partoo\NoSQL\Program.cs :ligne 17

Cette exception a été levée à l'origine dans cette pile des appels : LiteDB.Token.Expect(LiteDB.TokenType) dans Tokenizer.cs LiteDB.JsonReader.DeserializeArray() dans JsonReader.cs System.Linq.Enumerable.SelectEnumerableIterator<TSource, TResult>.MoveNext() LiteDB.LiteCollection.GetBsonDocs(System.Collections.Generic.IEnumerable) dans Insert.cs LiteDB.Engine.LiteEngine.Insert.AnonymousMethod__0(LiteDB.Engine.TransactionService) dans Insert.cs LiteDB.Engine.LiteEngine.AutoTransaction(System.Func<LiteDB.Engine.TransactionService, T>) dans Transaction.cs LiteDB.Engine.LiteEngine.Insert(string, System.Collections.Generic.IEnumerable, LiteDB.BsonAutoId) dans Insert.cs LiteDB.LiteCollection.Insert(System.Collections.Generic.IEnumerable) dans Insert.cs NoSQL.Program.Main(string[]) dans Program.cs

Additional context

FrenchFriend commented 2 years ago

mistake found, between content of file and name of file...