private JsonGoalList GetJson(string fullName)
 {
     if (File.Exists(fullName))
     {
         using (StreamReader reader = new StreamReader(fullName))
         {
             string json = reader.ReadToEnd();
             JsonSerializerSettings serSettings = new JsonSerializerSettings();
             serSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
             JsonGoalList tmpGoalList = JsonConvert.DeserializeObject <JsonGoalList>(json, serSettings);
             return(tmpGoalList);
         }
         //byte[] bytes = Encoding.Default.GetBytes(JsonConvert.SerializeObject(array));
         //await File.WriteAllBytesAsync(fullName, bytes);
     }
     return(null);
 }
        private async Task UpdateJson(JsonGoalList goalList, bool islocal, bool isWeb)
        {
            goalList.UpdateDate = DateTime.Now;
            if (islocal)
            {
                byte[] bytes = Encoding.Default.GetBytes(JsonConvert.SerializeObject(goalList));
                await File.WriteAllBytesAsync(jsonFullName, bytes);
            }

            if (isWeb)
            {
                IDiskApi diskApi = new DiskHttpApi(oauthToken);
                await diskApi.Files.UploadFileAsync(path : "/foo/Data.json",
                                                    overwrite : false,
                                                    localFile : jsonFullName,
                                                    cancellationToken : CancellationToken.None);
            }
        }
        private void InitializeJsomFile()
        {
            if (!Directory.Exists(jsonPath))
            {
                Directory.CreateDirectory(jsonPath);
            }


            //4 ситуации
            //1. Нет данных о data.json нигде
            //2. Данные есть только на сервере
            //3. Данные есть только в локальном каталоге (что врядли, либо если нет соединения)
            //4. Данные и там, и там (надо сравнивать)

            JsonGoalList jsonLocal = GetLocal();
            string       token     = "";

            if (jsonLocal != null)
            {
                token = jsonLocal.Token;
            }
            JsonGoalList jsonYa = GetFromYa(token);

            if (jsonLocal == null && jsonYa == null)
            {
                MessageBox.Show("No data");
                jsonCurrent = new JsonGoalList()
                {
                    Key = secret, UpdateDate = DateTime.Now
                };
                Task t = Task.Run(() => UpdateJson(jsonCurrent, true, true));
                // t.Wait();

                if (!t.IsCompletedSuccessfully)
                {
                    MessageBox.Show("Error: " + t.Status.ToString());
                }
            }
            else if (jsonLocal != null && jsonYa == null)
            {
                MessageBox.Show("Only local");
                jsonCurrent = jsonLocal;
                Task t = Task.Run(() => UpdateJson(jsonLocal, false, true));
                t.Wait(10000);
                if (!t.IsCompletedSuccessfully)
                {
                    MessageBox.Show("Error: " + t.Status.ToString());
                }
            }
            else if (jsonLocal == null && jsonYa != null)
            {
                jsonCurrent = jsonYa;
                MessageBox.Show("Only Ya");
                Task t = Task.Run(() => UpdateJson(jsonYa, true, false));
                if (!t.IsCompletedSuccessfully)
                {
                    MessageBox.Show("Error: " + t.Status.ToString());
                }
            }
            else
            {
                MessageBox.Show("Both availble");
                if (jsonYa.UpdateDate < jsonLocal.UpdateDate)
                {
                    Task t = Task.Run(() => UpdateJson(jsonLocal, false, true));
                    jsonCurrent = jsonLocal;
                }
                else
                {
                    Task t = Task.Run(() => UpdateJson(jsonYa, true, false));
                    jsonCurrent = jsonYa;
                }
            }
        }