json.inc 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * @file
  4. * The Node export JSON format handler.
  5. *
  6. * Adds JSON format to Node export.
  7. */
  8. /**
  9. * Export callback.
  10. */
  11. function node_export_json_export($nodes, $format) {
  12. return drupal_json_encode(node_export_json_encode_objects($nodes));
  13. }
  14. /**
  15. * Import callback.
  16. */
  17. function node_export_json_import($code_string) {
  18. return node_export_json_decode_objects(drupal_json_decode($code_string));
  19. }
  20. /**
  21. * Mark objects as being objects.
  22. */
  23. function node_export_json_encode_objects($var) {
  24. if (is_object($var)) {
  25. $var = (array)$var;
  26. $var['#node_export_object'] = '1';
  27. }
  28. if (is_array($var)) {
  29. foreach ($var as $key => $value) {
  30. $var[$key] = node_export_json_encode_objects($value);
  31. }
  32. }
  33. return $var;
  34. }
  35. /**
  36. * Recursively convert arrays back to objects.
  37. */
  38. function node_export_json_decode_objects($array) {
  39. if (is_array($array)) {
  40. foreach ($array as $k => $v) {
  41. if (is_array($v)) {
  42. $array[$k] = node_export_json_decode_objects($v);
  43. }
  44. }
  45. if (isset($array['#node_export_object'])) {
  46. unset($array['#node_export_object']);
  47. $array = (object)$array;
  48. }
  49. return $array;
  50. }
  51. }
  52. /**
  53. * Callback for actions.
  54. */
  55. function node_export_json_action_form($context, &$form_state) {
  56. return node_export_action_form($context, $form_state, 'json');
  57. }