diff options
author | Christian Grothoff <grothoff@gnunet.org> | 2021-07-26 15:31:14 +0200 |
---|---|---|
committer | Christian Grothoff <grothoff@gnunet.org> | 2021-07-26 15:31:14 +0200 |
commit | 51990dbbf5b7d5332526ed31285f50a5fcb75666 (patch) | |
tree | 6425f01fd0b59f9014397bc96b17a394dfcded56 /src/include/gnunet_json_lib.h | |
parent | 8be49ca6ed306bc8c5833fb006b22f1099df93ba (diff) | |
download | gnunet-51990dbbf5b7d5332526ed31285f50a5fcb75666.tar.gz gnunet-51990dbbf5b7d5332526ed31285f50a5fcb75666.zip |
JSON: add draft for new GNUNET_JSON_PACK API
Diffstat (limited to 'src/include/gnunet_json_lib.h')
-rw-r--r-- | src/include/gnunet_json_lib.h | 318 |
1 files changed, 318 insertions, 0 deletions
diff --git a/src/include/gnunet_json_lib.h b/src/include/gnunet_json_lib.h index 3dc79105e..7cb687593 100644 --- a/src/include/gnunet_json_lib.h +++ b/src/include/gnunet_json_lib.h | |||
@@ -521,6 +521,324 @@ GNUNET_JSON_getopt (char shortName, | |||
521 | const char *description, | 521 | const char *description, |
522 | json_t **json); | 522 | json_t **json); |
523 | 523 | ||
524 | |||
525 | /* ****************** JSON PACK helper ******************* */ | ||
526 | |||
527 | |||
528 | /** | ||
529 | * Element in the array to give to the packer. | ||
530 | */ | ||
531 | struct GNUNET_JSON_PackSpec; | ||
532 | |||
533 | |||
534 | /** | ||
535 | * Function called to pack an element into the JSON | ||
536 | * object as part of #GNUNET_JSON_pack_(). | ||
537 | * | ||
538 | * @param se pack specification to execute | ||
539 | * @return json object to pack, NULL to pack nothing | ||
540 | */ | ||
541 | typedef json_t * | ||
542 | (GNUNET_JSON_PackCallback)(const struct GNUNET_JSON_PackSpec *se); | ||
543 | |||
544 | |||
545 | /** | ||
546 | * Element in the array to give to the packer. | ||
547 | */ | ||
548 | struct GNUNET_JSON_PackSpec | ||
549 | { | ||
550 | /** | ||
551 | * Name of the field to pack. | ||
552 | */ | ||
553 | const char *field_name; | ||
554 | |||
555 | /** | ||
556 | * Function that will do the packing. | ||
557 | */ | ||
558 | GNUNET_JSON_PackCallback packer; | ||
559 | |||
560 | /** | ||
561 | * Closure for @e packer. | ||
562 | */ | ||
563 | void *packer_cls; | ||
564 | |||
565 | /** | ||
566 | * Pointer to be provided to the packer. | ||
567 | */ | ||
568 | const void *value_ptr; | ||
569 | |||
570 | /** | ||
571 | * Numeric value for the packer, could be | ||
572 | * the length of the data in @e value_ptr, | ||
573 | * or something different depending on the type. | ||
574 | */ | ||
575 | uint64_t value_num; | ||
576 | |||
577 | /** | ||
578 | * True if a NULL (or 0) argument is allowed. In this | ||
579 | * case, if the argument is NULL the @e packer should | ||
580 | * return NULL and the field should be skipped (omitted from | ||
581 | * the generated object) and not be serialized at all. | ||
582 | */ | ||
583 | bool allow_null; | ||
584 | }; | ||
585 | |||
586 | |||
587 | /** | ||
588 | * Pack a JSON object from a @a spec. Aborts if | ||
589 | * packing fails. | ||
590 | * | ||
591 | * @param spec specification object | ||
592 | * @return JSON object | ||
593 | */ | ||
594 | json_t * | ||
595 | GNUNET_JSON_pack_ (struct GNUNET_JSON_PackSpec spec[]); | ||
596 | |||
597 | |||
598 | /** | ||
599 | * Pack a JSON object from a @a spec. Aborts if | ||
600 | * packing fails. | ||
601 | * | ||
602 | * @param ... list of specification objects | ||
603 | * @return JSON object | ||
604 | */ | ||
605 | #define GNUNET_JSON_PACK(...) \ | ||
606 | GNUNET_JSON_pack_ ((struct GNUNET_JSON_PackSpec[]) {__VA_ARGS__, GNUNET_JSON_pack_end_ ()}) | ||
607 | |||
608 | |||
609 | /** | ||
610 | * Do not use directly. Use #GNUNET_JSON_PACK. | ||
611 | * | ||
612 | * @return array terminator | ||
613 | */ | ||
614 | struct GNUNET_JSON_PackSpec | ||
615 | GNUNET_JSON_pack_end_(void); | ||
616 | |||
617 | |||
618 | /** | ||
619 | * Modify packer instruction to allow NULL as a value. | ||
620 | * | ||
621 | * @param in json pack specification to modify | ||
622 | * @return json pack specification | ||
623 | */ | ||
624 | struct GNUNET_JSON_PackSpec | ||
625 | GNUNET_JSON_pack_allow_null (struct GNUNET_JSON_PackSpec in); | ||
626 | |||
627 | |||
628 | /** | ||
629 | * Generate packer instruction for a JSON field of type | ||
630 | * bool. | ||
631 | * | ||
632 | * @param name name of the field to add to the object | ||
633 | * @param b boolean value | ||
634 | * @return json pack specification | ||
635 | */ | ||
636 | struct GNUNET_JSON_PackSpec | ||
637 | GNUNET_JSON_pack_bool (const char *name, | ||
638 | bool b); | ||
639 | |||
640 | |||
641 | /** | ||
642 | * Generate packer instruction for a JSON field of type | ||
643 | * string. | ||
644 | * | ||
645 | * @param name name of the field to add to the object | ||
646 | * @param s string value | ||
647 | * @return json pack specification | ||
648 | */ | ||
649 | struct GNUNET_JSON_PackSpec | ||
650 | GNUNET_JSON_pack_string (const char *name, | ||
651 | const char *s); | ||
652 | |||
653 | |||
654 | /** | ||
655 | * Generate packer instruction for a JSON field of type | ||
656 | * unsigned integer. Note that the maximum allowed | ||
657 | * value is still limited by JSON and not UINT64_MAX. | ||
658 | * | ||
659 | * @param name name of the field to add to the object | ||
660 | * @param num numeric value | ||
661 | * @return json pack specification | ||
662 | */ | ||
663 | struct GNUNET_JSON_PackSpec | ||
664 | GNUNET_JSON_pack_uint64 (const char *name, | ||
665 | uint64_t num); | ||
666 | |||
667 | |||
668 | /** | ||
669 | * Generate packer instruction for a JSON field of type | ||
670 | * signed integer. | ||
671 | * | ||
672 | * @param name name of the field to add to the object | ||
673 | * @param num numeric value | ||
674 | * @return json pack specification | ||
675 | */ | ||
676 | struct GNUNET_JSON_PackSpec | ||
677 | GNUNET_JSON_pack_int64 (const char *name, | ||
678 | int64_t num); | ||
679 | |||
680 | |||
681 | /** | ||
682 | * Generate packer instruction for a JSON field of type | ||
683 | * JSON object where the reference is taken over by | ||
684 | * the packer. | ||
685 | * | ||
686 | * @param name name of the field to add to the object | ||
687 | * @param o object to steal | ||
688 | * @return json pack specification | ||
689 | */ | ||
690 | struct GNUNET_JSON_PackSpec | ||
691 | GNUNET_JSON_pack_object_steal (const char *name, | ||
692 | json_t *o); | ||
693 | |||
694 | |||
695 | /** | ||
696 | * Generate packer instruction for a JSON field of type JSON object where the | ||
697 | * reference counter is incremented by the packer. Note that a deep copy is | ||
698 | * not performed. | ||
699 | * | ||
700 | * @param name name of the field to add to the object | ||
701 | * @param o object to increment reference counter of | ||
702 | * @return json pack specification | ||
703 | */ | ||
704 | struct GNUNET_JSON_PackSpec | ||
705 | GNUNET_JSON_pack_object_incref (const char *name, | ||
706 | json_t *o); | ||
707 | |||
708 | |||
709 | /** | ||
710 | * Generate packer instruction for a JSON field of type | ||
711 | * JSON array where the reference is taken over by | ||
712 | * the packer. | ||
713 | * | ||
714 | * @param name name of the field to add to the object | ||
715 | * @param a array to steal | ||
716 | * @return json pack specification | ||
717 | */ | ||
718 | struct GNUNET_JSON_PackSpec | ||
719 | GNUNET_JSON_pack_array_steal (const char *name, | ||
720 | json_t *a); | ||
721 | |||
722 | |||
723 | /** | ||
724 | * Generate packer instruction for a JSON field of type JSON array where the | ||
725 | * reference counter is incremented by the packer. Note that a deep copy is | ||
726 | * not performed. | ||
727 | * | ||
728 | * @param name name of the field to add to the object | ||
729 | * @param a array to increment reference counter of | ||
730 | * @return json pack specification | ||
731 | */ | ||
732 | struct GNUNET_JSON_PackSpec | ||
733 | GNUNET_JSON_pack_array_incref (const char *name, | ||
734 | json_t *a); | ||
735 | |||
736 | |||
737 | /** | ||
738 | * Generate packer instruction for a JSON field of type | ||
739 | * variable size binary blob. | ||
740 | * | ||
741 | * @param name name of the field to add to the object | ||
742 | * @param blob binary data to pack | ||
743 | * @param blob_size number of bytes in @a blob | ||
744 | * @return json pack specification | ||
745 | */ | ||
746 | struct GNUNET_JSON_PackSpec | ||
747 | GNUNET_JSON_pack_data_varsize (const char *name, | ||
748 | const void *blob, | ||
749 | size_t blob_size); | ||
750 | |||
751 | |||
752 | /** | ||
753 | * Generate packer instruction for a JSON field where the | ||
754 | * size is automatically determined from the argument. | ||
755 | * | ||
756 | * @param name name of the field to add to the object | ||
757 | * @param blob data to pack, must not be an array | ||
758 | * @return json pack specification | ||
759 | */ | ||
760 | #define GNUNET_JSON_pack_data_auto(name,blob) \ | ||
761 | GNUNET_JSON_pack_data_varsize (name, blob, sizeof (*blob)) | ||
762 | |||
763 | |||
764 | /** | ||
765 | * Generate packer instruction for a JSON field of type | ||
766 | * absolute time. | ||
767 | * | ||
768 | * @param name name of the field to add to the object | ||
769 | * @param at absolute time to pack | ||
770 | * @return json pack specification | ||
771 | */ | ||
772 | struct GNUNET_JSON_PackSpec | ||
773 | GNUNET_JSON_pack_time_abs (const char *name, | ||
774 | struct GNUNET_TIME_Absolute at); | ||
775 | |||
776 | |||
777 | /** | ||
778 | * Generate packer instruction for a JSON field of type | ||
779 | * absolute time in network byte order. | ||
780 | * | ||
781 | * @param name name of the field to add to the object | ||
782 | * @param at absolute time to pack | ||
783 | * @return json pack specification | ||
784 | */ | ||
785 | struct GNUNET_JSON_PackSpec | ||
786 | GNUNET_JSON_pack_time_abs_nbo (const char *name, | ||
787 | struct GNUNET_TIME_AbsoluteNBO at); | ||
788 | |||
789 | |||
790 | /** | ||
791 | * Generate packer instruction for a JSON field of type | ||
792 | * relative time. | ||
793 | * | ||
794 | * @param name name of the field to add to the object | ||
795 | * @param rt relative time to pack | ||
796 | * @return json pack specification | ||
797 | */ | ||
798 | struct GNUNET_JSON_PackSpec | ||
799 | GNUNET_JSON_pack_time_rel (const char *name, | ||
800 | struct GNUNET_TIME_Relative rt); | ||
801 | |||
802 | |||
803 | /** | ||
804 | * Generate packer instruction for a JSON field of type | ||
805 | * relative time in network byte order. | ||
806 | * | ||
807 | * @param name name of the field to add to the object | ||
808 | * @param rt relative time to pack | ||
809 | * @return json pack specification | ||
810 | */ | ||
811 | struct GNUNET_JSON_PackSpec | ||
812 | GNUNET_JSON_pack_time_rel (const char *name, | ||
813 | struct GNUNET_TIME_RelativeNBO rt); | ||
814 | |||
815 | |||
816 | /** | ||
817 | * Generate packer instruction for a JSON field of type | ||
818 | * RSA public key. | ||
819 | * | ||
820 | * @param name name of the field to add to the object | ||
821 | * @param pk RSA public key | ||
822 | * @return json pack specification | ||
823 | */ | ||
824 | struct GNUNET_JSON_PackSpec | ||
825 | GNUNET_JSON_pack_rsa_public_key (const char *name, | ||
826 | const struct GNUNET_CRYPTO_RsaPublicKey *pk); | ||
827 | |||
828 | |||
829 | /** | ||
830 | * Generate packer instruction for a JSON field of type | ||
831 | * RSA signature. | ||
832 | * | ||
833 | * @param name name of the field to add to the object | ||
834 | * @param sig RSA signature | ||
835 | * @return json pack specification | ||
836 | */ | ||
837 | struct GNUNET_JSON_PackSpec | ||
838 | GNUNET_JSON_pack_ (const char *name, | ||
839 | const struct GNUNET_CRYPTO_RsaSignature *sig); | ||
840 | |||
841 | |||
524 | #endif | 842 | #endif |
525 | 843 | ||
526 | /* end of gnunet_json_lib.h */ | 844 | /* end of gnunet_json_lib.h */ |