Сохранить строку JSON (из холста) в таблице MySQL

Я хочу сохранить строку json в таблице MySQL. Строка json исходит из холста (с использованием Fabricjs).

var jsonCanvas = JSON.stringify(canvas.toDatalessObject());

Мой первый вопрос: какой тип данных лучше всего подходит для сохранения строки json в MySQL? Холст будет содержать изображения, а также текст и другие объекты. Как вы думаете, достаточно ли MEDIUMTEXT?

Другой вопрос. Можно ли сохранить строку json с помощью этого подготовленного оператора. Я сомневаюсь, чтобы использовать ключевое слово "s" для строки.

try {
  $sql = "insert into layout (lt_author, lt_date, lt_category, lt_json_string)
   values (?,?,?,?)";
  $stmt = $mysqli->prepare($sql);
  $stmt->bind_param("ssss",$lt_author, $lt_date, $lt_category, $lt_json_string);
  $stmt->execute();
  $stmt->close();

Спасибо за помощь :) Привет Макс


person droiddude    schedule 28.12.2012    source источник
comment
Объект JSON будет содержать фактические данные двоичного изображения? Или URL-адрес, по которому находится изображение. Если первое, то вам нужно проявлять осторожность при преобразовании двоичных данных в строку.   -  person Perception    schedule 28.12.2012


Ответы (1)


I'm assuming that your SQL is correct.
I always chose TEXT to store my json string representation of my canvas.
But there is somethings to consider before you chose how to save your JSON representation.
See this discussion here: Loading JSON in canvas with fabric.js
It's explained the main points that you need observe.

  • Используйте .toDatalessObject() вместо .toJson() метода:
  • - Pros: As kangax explained in the discussion, toDatalessObject will save just the url source of your svg, so, less data to be saved in your database. By this way, you can maybe use other type to store your json canvas representation (maybe you can use a TINYTEXT type)
    - Cons: You need have the files in your server, because you will reference it when load the canvas again.

  • Сколько объектов будет на вашем холсте? И что это за объекты? Больше svg или изображения и текста?
  • You need know because, if you use .toDatalessJson() method and you will have more svg loaded in your canvas, this svg will be converted in Path and PathGroup object, and will be save in your database.
    So, if you have many, many svg in your canvas, maybe the final json string can overflow the size allowed for types TEXT in database (if this happen, there some solutions, like split your json string representation and save in two or more entries in your database).
    But, if this happen frequently, maybe will better chose other type, like MEDIUMTEXT, as you suggest.

    person rodrigopandini    schedule 06.01.2013