| 764 | |
| 765 | |
| 766 | Try<vector<Resource>> Resources::fromJSON( |
| 767 | const JSON::Array& resourcesJSON, |
| 768 | const string& defaultRole) |
| 769 | { |
| 770 | // Convert the JSON Array into a protobuf message and use |
| 771 | // that to construct a vector of Resource object. |
| 772 | Try<RepeatedPtrField<Resource>> resourcesProtobuf = |
| 773 | protobuf::parse<RepeatedPtrField<Resource>>(resourcesJSON); |
| 774 | |
| 775 | if (resourcesProtobuf.isError()) { |
| 776 | return Error( |
| 777 | "Some JSON resources were not formatted properly: " + |
| 778 | resourcesProtobuf.error()); |
| 779 | } |
| 780 | |
| 781 | vector<Resource> result; |
| 782 | |
| 783 | foreach (Resource& resource, resourcesProtobuf.get()) { |
| 784 | // Set the default role if none was specified. |
| 785 | // |
| 786 | // NOTE: We rely on the fact that the result of this function is |
| 787 | // converted to the "post-reservation-refinement" format. |
| 788 | if (!resource.has_role() && resource.reservations_size() == 0) { |
| 789 | resource.set_role(defaultRole); |
| 790 | } |
| 791 | |
| 792 | // We add the Resource object even if it is empty or invalid. |
| 793 | result.push_back(resource); |
| 794 | } |
| 795 | |
| 796 | return result; |
| 797 | } |
| 798 | |
| 799 | |
| 800 | Try<vector<Resource>> Resources::fromSimpleString( |