Haskell, Aeson - no instance for (ToJSON ByteString)












2















So happy making it this far, encountered a new hurdle:
Got this code made to be encoded to JSON. However no matter when type I use as an instance, the compiler complains. Now I am obviously doing something wrong, but it is exactly what is in the documentation (when using DeriveGeneric obviously).



{-# LANGUAGE OverloadedStrings, DeriveGeneric #-}

import Data.Aeson
import Data.Text as T
import Data.ByteString.Lazy as B
import Data.ByteString.Lazy.Char8 as BC
import GHC.Generics

-- decode :: FromJSON a => B.ByteString -> Maybe a
-- decode' :: FromJSON a => B.ByteString -> Either String a
-- encode :: ToJSON => a -> B.ByteString

data System = System { system :: BC.ByteString
, make :: BC.ByteString
, code :: Int
} deriving (Generic, Show)
instance ToJSON System
-- instance FromJSON System

platform = System { system = "FPGA"
, make = "Xilinx"
, code = 10165
}
encodePlatform :: BC.ByteString
encodePlatform = encode platform


Compiler output:



    • No instance for (ToJSON ByteString)
arising from a use of ‘aeson-1.4.1.0:Data.Aeson.Types.ToJSON.$dmtoJSON’
• In the expression:
aeson-1.4.1.0:Data.Aeson.Types.ToJSON.$dmtoJSON @(System)
In an equation for ‘toJSON’:
toJSON = aeson-1.4.1.0:Data.Aeson.Types.ToJSON.$dmtoJSON @(System)
In the instance declaration for ‘ToJSON System’
|
17 | instance ToJSON System









share|improve this question


















  • 1





    If ByteString is really the correct type here, then you should probably not be converting it to JSON. Use binary serialisation instead. If those fields actually contain text rather than binary data, then make the type Text. If they do contain binary data but you need to use JSON, use base64 or something like that.

    – leftaroundabout
    Nov 14 '18 at 8:42
















2















So happy making it this far, encountered a new hurdle:
Got this code made to be encoded to JSON. However no matter when type I use as an instance, the compiler complains. Now I am obviously doing something wrong, but it is exactly what is in the documentation (when using DeriveGeneric obviously).



{-# LANGUAGE OverloadedStrings, DeriveGeneric #-}

import Data.Aeson
import Data.Text as T
import Data.ByteString.Lazy as B
import Data.ByteString.Lazy.Char8 as BC
import GHC.Generics

-- decode :: FromJSON a => B.ByteString -> Maybe a
-- decode' :: FromJSON a => B.ByteString -> Either String a
-- encode :: ToJSON => a -> B.ByteString

data System = System { system :: BC.ByteString
, make :: BC.ByteString
, code :: Int
} deriving (Generic, Show)
instance ToJSON System
-- instance FromJSON System

platform = System { system = "FPGA"
, make = "Xilinx"
, code = 10165
}
encodePlatform :: BC.ByteString
encodePlatform = encode platform


Compiler output:



    • No instance for (ToJSON ByteString)
arising from a use of ‘aeson-1.4.1.0:Data.Aeson.Types.ToJSON.$dmtoJSON’
• In the expression:
aeson-1.4.1.0:Data.Aeson.Types.ToJSON.$dmtoJSON @(System)
In an equation for ‘toJSON’:
toJSON = aeson-1.4.1.0:Data.Aeson.Types.ToJSON.$dmtoJSON @(System)
In the instance declaration for ‘ToJSON System’
|
17 | instance ToJSON System









share|improve this question


















  • 1





    If ByteString is really the correct type here, then you should probably not be converting it to JSON. Use binary serialisation instead. If those fields actually contain text rather than binary data, then make the type Text. If they do contain binary data but you need to use JSON, use base64 or something like that.

    – leftaroundabout
    Nov 14 '18 at 8:42














2












2








2








So happy making it this far, encountered a new hurdle:
Got this code made to be encoded to JSON. However no matter when type I use as an instance, the compiler complains. Now I am obviously doing something wrong, but it is exactly what is in the documentation (when using DeriveGeneric obviously).



{-# LANGUAGE OverloadedStrings, DeriveGeneric #-}

import Data.Aeson
import Data.Text as T
import Data.ByteString.Lazy as B
import Data.ByteString.Lazy.Char8 as BC
import GHC.Generics

-- decode :: FromJSON a => B.ByteString -> Maybe a
-- decode' :: FromJSON a => B.ByteString -> Either String a
-- encode :: ToJSON => a -> B.ByteString

data System = System { system :: BC.ByteString
, make :: BC.ByteString
, code :: Int
} deriving (Generic, Show)
instance ToJSON System
-- instance FromJSON System

platform = System { system = "FPGA"
, make = "Xilinx"
, code = 10165
}
encodePlatform :: BC.ByteString
encodePlatform = encode platform


Compiler output:



    • No instance for (ToJSON ByteString)
arising from a use of ‘aeson-1.4.1.0:Data.Aeson.Types.ToJSON.$dmtoJSON’
• In the expression:
aeson-1.4.1.0:Data.Aeson.Types.ToJSON.$dmtoJSON @(System)
In an equation for ‘toJSON’:
toJSON = aeson-1.4.1.0:Data.Aeson.Types.ToJSON.$dmtoJSON @(System)
In the instance declaration for ‘ToJSON System’
|
17 | instance ToJSON System









share|improve this question














So happy making it this far, encountered a new hurdle:
Got this code made to be encoded to JSON. However no matter when type I use as an instance, the compiler complains. Now I am obviously doing something wrong, but it is exactly what is in the documentation (when using DeriveGeneric obviously).



{-# LANGUAGE OverloadedStrings, DeriveGeneric #-}

import Data.Aeson
import Data.Text as T
import Data.ByteString.Lazy as B
import Data.ByteString.Lazy.Char8 as BC
import GHC.Generics

-- decode :: FromJSON a => B.ByteString -> Maybe a
-- decode' :: FromJSON a => B.ByteString -> Either String a
-- encode :: ToJSON => a -> B.ByteString

data System = System { system :: BC.ByteString
, make :: BC.ByteString
, code :: Int
} deriving (Generic, Show)
instance ToJSON System
-- instance FromJSON System

platform = System { system = "FPGA"
, make = "Xilinx"
, code = 10165
}
encodePlatform :: BC.ByteString
encodePlatform = encode platform


Compiler output:



    • No instance for (ToJSON ByteString)
arising from a use of ‘aeson-1.4.1.0:Data.Aeson.Types.ToJSON.$dmtoJSON’
• In the expression:
aeson-1.4.1.0:Data.Aeson.Types.ToJSON.$dmtoJSON @(System)
In an equation for ‘toJSON’:
toJSON = aeson-1.4.1.0:Data.Aeson.Types.ToJSON.$dmtoJSON @(System)
In the instance declaration for ‘ToJSON System’
|
17 | instance ToJSON System






haskell bytestring aeson






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 8:33









MadderoteMadderote

197110




197110








  • 1





    If ByteString is really the correct type here, then you should probably not be converting it to JSON. Use binary serialisation instead. If those fields actually contain text rather than binary data, then make the type Text. If they do contain binary data but you need to use JSON, use base64 or something like that.

    – leftaroundabout
    Nov 14 '18 at 8:42














  • 1





    If ByteString is really the correct type here, then you should probably not be converting it to JSON. Use binary serialisation instead. If those fields actually contain text rather than binary data, then make the type Text. If they do contain binary data but you need to use JSON, use base64 or something like that.

    – leftaroundabout
    Nov 14 '18 at 8:42








1




1





If ByteString is really the correct type here, then you should probably not be converting it to JSON. Use binary serialisation instead. If those fields actually contain text rather than binary data, then make the type Text. If they do contain binary data but you need to use JSON, use base64 or something like that.

– leftaroundabout
Nov 14 '18 at 8:42





If ByteString is really the correct type here, then you should probably not be converting it to JSON. Use binary serialisation instead. If those fields actually contain text rather than binary data, then make the type Text. If they do contain binary data but you need to use JSON, use base64 or something like that.

– leftaroundabout
Nov 14 '18 at 8:42












1 Answer
1






active

oldest

votes


















6














That's because there is no ByteString instance for ToJSON typeclass. Historically, it used to be present but it was removed because JSON strings should be valid unicode.



You can find more details here:




  • https://github.com/bos/aeson/issues/126

  • https://github.com/bos/aeson/commit/9ee73f303cacb8ae73b2325f2f47288522607420

  • https://github.com/bos/aeson/pull/148/files


For fixing it, I would convert it to Text type and then encode into JSON.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53295922%2fhaskell-aeson-no-instance-for-tojson-bytestring%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    6














    That's because there is no ByteString instance for ToJSON typeclass. Historically, it used to be present but it was removed because JSON strings should be valid unicode.



    You can find more details here:




    • https://github.com/bos/aeson/issues/126

    • https://github.com/bos/aeson/commit/9ee73f303cacb8ae73b2325f2f47288522607420

    • https://github.com/bos/aeson/pull/148/files


    For fixing it, I would convert it to Text type and then encode into JSON.






    share|improve this answer






























      6














      That's because there is no ByteString instance for ToJSON typeclass. Historically, it used to be present but it was removed because JSON strings should be valid unicode.



      You can find more details here:




      • https://github.com/bos/aeson/issues/126

      • https://github.com/bos/aeson/commit/9ee73f303cacb8ae73b2325f2f47288522607420

      • https://github.com/bos/aeson/pull/148/files


      For fixing it, I would convert it to Text type and then encode into JSON.






      share|improve this answer




























        6












        6








        6







        That's because there is no ByteString instance for ToJSON typeclass. Historically, it used to be present but it was removed because JSON strings should be valid unicode.



        You can find more details here:




        • https://github.com/bos/aeson/issues/126

        • https://github.com/bos/aeson/commit/9ee73f303cacb8ae73b2325f2f47288522607420

        • https://github.com/bos/aeson/pull/148/files


        For fixing it, I would convert it to Text type and then encode into JSON.






        share|improve this answer















        That's because there is no ByteString instance for ToJSON typeclass. Historically, it used to be present but it was removed because JSON strings should be valid unicode.



        You can find more details here:




        • https://github.com/bos/aeson/issues/126

        • https://github.com/bos/aeson/commit/9ee73f303cacb8ae73b2325f2f47288522607420

        • https://github.com/bos/aeson/pull/148/files


        For fixing it, I would convert it to Text type and then encode into JSON.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 14 '18 at 10:39

























        answered Nov 14 '18 at 8:38









        SibiSibi

        30.2k965124




        30.2k965124






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53295922%2fhaskell-aeson-no-instance-for-tojson-bytestring%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python